Java Program to Find the Quotient and Remainder
When you divide one whole number (the dividend) by another (the divisor), you get a quotient and a remainder. For 40 ÷ 6, the quotient is 6 and the remainder is 4, because 6 × 6 + 4 = 40.
In Java, with int values:
dividend / divisorgives the quotient (the decimal part is dropped),dividend % divisorgives the remainder.
1. Find the Quotient and Remainder
public class QuotientRemainder {
public static void main(String[] args) {
int dividend = 40;
int divisor = 6;
int quotient = dividend / divisor;
int remainder = dividend % divisor;
System.out.println("Dividend: " + dividend + ", Divisor: " + divisor);
System.out.println("Quotient: " + quotient);
System.out.println("Remainder: " + remainder);
System.out.println("Check: " + divisor + " x " + quotient + " + " + remainder + " = " + (divisor * quotient + remainder));
}
}
Dividend: 40, Divisor: 6 Quotient: 6 Remainder: 4 Check: 6 x 6 + 4 = 40
2. Quotient and Remainder with User Input
import java.util.Scanner;
public class QuotientRemainderInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the dividend: ");
int dividend = scanner.nextInt();
System.out.print("Enter the divisor: ");
int divisor = scanner.nextInt();
scanner.close();
if (divisor == 0) {
System.out.println("Cannot divide by zero.");
return;
}
System.out.println("Quotient = " + (dividend / divisor));
System.out.println("Remainder = " + (dividend % divisor));
}
}
Enter the dividend: 31 Enter the divisor: 6 Quotient = 5 Remainder = 1
Enter the dividend: 31 Enter the divisor: 0 Cannot divide by zero.
3. Quotient and Remainder with Negative Numbers
In Java, integer division rounds toward zero, and the remainder always has the same sign as the dividend. Math.floorDiv() and Math.floorMod() round down instead, which is useful when you always want a non-negative remainder for a positive divisor.
public class NegativeDivision {
public static void main(String[] args) {
System.out.println(" 17 / 5 = " + (17 / 5) + ", 17 % 5 = " + (17 % 5));
System.out.println("-17 / 5 = " + (-17 / 5) + ", -17 % 5 = " + (-17 % 5));
System.out.println("floorDiv(-17, 5) = " + Math.floorDiv(-17, 5)
+ ", floorMod(-17, 5) = " + Math.floorMod(-17, 5));
}
}
17 / 5 = 3, 17 % 5 = 2 -17 / 5 = -3, -17 % 5 = -2 floorDiv(-17, 5) = -4, floorMod(-17, 5) = 3
4. Division by Zero Throws ArithmeticException
Dividing an int by zero is an error: Java throws an ArithmeticException and, if it is not caught, stops the program. Check the divisor before dividing, as in the user input example above.
public class DivideByZero {
public static void main(String[] args) {
int dividend = 25;
int divisor = 0;
System.out.println("Quotient: " + (dividend / divisor));
}
}
Exception in thread "main" java.lang.ArithmeticException: / by zero at DivideByZero.main(DivideByZero.java:6)
With double values there is no exception: 25.0 / 0 gives Infinity.
5. Summary
/on integers gives the quotient and%gives the remainder.- The results always satisfy
divisor * quotient + remainder == dividend. - With negative numbers, the remainder takes the sign of the dividend;
Math.floorMod()gives a non-negative one. - Integer division by zero throws
ArithmeticException, so check the divisor first.