Google News
logo
Java Program to Compute Quotient and Remainder
In the following example Java program to compute the quotient and remainder of two numbers :
Program :
import java.util.Scanner;

public class QuotientRemainder {

    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();

        int quotient = dividend / divisor;
        int remainder = dividend % divisor;

        System.out.println("The quotient of " + dividend + " divided by " + divisor + " is " + quotient);
        System.out.println("The remainder of " + dividend + " divided by " + divisor + " is " + remainder);
    }
}
Output :
Enter the dividend: 24
Enter the divisor: 5
The quotient of 24 divided by 5 is 4
The remainder of 24 divided by 5 is 4
In this program, we use the Scanner class to read two numbers input by the user. We then use the division and modulo operators (/ and %) to compute the quotient and remainder of the two numbers.

We store the quotient in a variable called quotient and the remainder in a variable called remainder. Finally, we print out a message displaying the values of the dividend, divisor, quotient, and remainder.