Java Program to Compute Quotient and Remainder

Write a Java Program to compute the quotient and remainder with an example. In this programming language, we have / and % operators to find the quotient and remainder. This example allows the user to enter two integer values and calculates the same.

package SimpleNumberPrograms;

import java.util.Scanner;

public class QuotientRemainder {
	private static Scanner sc;

	public static void main(String[] args) {
		int num1, num2, quotient, remainder;
		sc = new Scanner(System.in);
		
		System.out.print("Enter the First Value =  ");
		num1 = sc.nextInt();

		System.out.print("Enter the Second Value = ");
		num2 = sc.nextInt();
		
		quotient = num1 / num2;
		remainder = num1 % num2;
		
		System.out.printf("\nQuotient of %d and %d = %d", num1, num2, quotient);
		System.out.printf("\nRemainder of %d and %d = %d", num1, num2, remainder);
	}
}
Java Program to Compute Quotient and Remainder 1

Let me try another value.

Enter the First Value =  200
Enter the Second Value = 5

Quotient of 200 and 5 = 40
Remainder of 200 and 5 = 0

Java Program to Compute Quotient and Remainder using functions

In this example, we created calcQuo and calcRem functions that compute and return the quotient and remainder.

package SimpleNumberPrograms;

import java.util.Scanner;

public class QuRem2 {
	private static Scanner sc;

	public static void main(String[] args) {
		int num1, num2;
		
		sc = new Scanner(System.in);
		
		System.out.print("Enter the First Value =  ");
		num1 = sc.nextInt();

		System.out.print("Enter the Second Value = ");
		num2 = sc.nextInt();
		
		System.out.printf("\nQuotient  = %d", calcQuo(num1, num2));
		System.out.printf("\nRemainder = %d", calcRem(num1, num2));
	}
	
	public static int calcQuo(int num1, int num2) {
		return num1 / num2;
	}
	
	public static int calcRem(int num1, int num2) {
		return num1 % num2;
	}
}
Enter the First Value =  125
Enter the Second Value = 7

Quotient  = 17
Remainder = 6