Calculate Quotient and Remainder in Java

Exercise:

Write a Java Program to calculate quotient and remainder of 2 integers and print the results.

Click Here to View the Solution!
public class QuotientRemainder {
 
    public static void main(String[] args) {
 
        int dividend = 37, divisor = 5;
 
        int quotient = dividend / divisor;
        int remainder = dividend % divisor;
 
        System.out.println("Quotient = " + quotient);
        System.out.println("Remainder = " + remainder);
    }
} 
Click Here to View the Output!
Quotient = 7
Remainder = 2
Click Here to View the Explanation!
  • In this program, two variables dividend and divisor are declared and then 37 and 5 are stored in them respectively.
  • Now, to find the quotient we divide dividend by divisor using operator.
  • Since both variables are integers the resultant will be computed as an integer as well.
  • Mathematically 37/5 is equal to  7.4, but as we used integer data type to store dividend and divisor  so the int quotient variable only stores 7 (integer part of answer).
  • Likewise we used (Modulus) operator to find the remainder. So, the remainder of 37/5, i.e. 2 is stored in an integer in variable remainder.
  • Finally println() function prints  quotient and remainder  on console screen.
%d bloggers like this: