Java Program to Compute Quotient and Remainder

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

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

In this Java example, we created calcQuotient and calcRemainder functions that compute and return the quotient and remainder.

package SimpleNumberPrograms;

import java.util.Scanner;

public class QuotientRemainder2 {
	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", calcQuotient(num1, num2));
		System.out.printf("\nRemainder = %d", calcRemainder(num1, num2));
	}
	
	public static int calcQuotient(int num1, int num2) {
		return num1 / num2;
	}
	
	public static int calcRemainder(int num1, int num2) {
		return num1 % num2;
	}
}
Enter the First Value =  125
Enter the Second Value = 7

Quotient  = 17
Remainder = 6