Quotient and remainder

This code performs division of two numbers and computes the quotient and remainder.

Code Example

import java.util.Scanner;

class QuotientReminder {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);

		int dividend, divisor, quotient, remainder;

		System.out.print("Please enter dividend: ");
		dividend = reader.nextInt();
		
		System.out.print("Please enter divisor: ");
		divisor = reader.nextInt();
		reader.close();
	
		quotient = dividend / divisor;
		remainder = dividend % divisor;
	
		System.out.println("Quotient is: " + quotient);
		System.out.print("Remainder is: " + remainder);
	}
}
Output
Please enter dividend: 118
Please enter divisor: 12
Quotient is: 9
Remainder is: 10

Code Explanation

import java.util.Scanner; imports the Scanner class from the java.util package. This class is used to read user input from the console.

class QuotientReminder declares a class named QuotientReminder.

public static void main(String[] args) is the main method of the program where the execution starts. It takes an array of strings as a parameter (although it is not used in this program).

The following lines declare variables:

Scanner reader = new Scanner(System.in);
int dividend, divisor, quotient, remainder;

Here, reader is an instance of the Scanner class that is used to read user input. dividend and divisor are int variables to store the dividend and divisor entered by the user, respectively. quotient and remainder are int variables to store the calculated quotient and remainder values.

System.out.print("Please enter dividend: "); displays a prompt message asking the user to enter the dividend.

dividend = reader.nextInt(); reads an integer value entered by the user using the nextInt() method of the Scanner class. The value entered by the user is stored in the dividend variable.

System.out.print("Please enter divisor: "); displays a prompt message asking the user to enter the divisor.

divisor = reader.nextInt(); reads an integer value entered by the user using the nextInt() method of the Scanner class. The value entered by the user is stored in the divisor variable.

reader.close(); closes the Scanner object to release system resources after reading the user input.

quotient = dividend / divisor; calculates the quotient of the division operation by dividing the dividend by the divisor. Since both dividend and divisor are integers, the division operation performs integer division, discarding any remainder.

remainder = dividend % divisor; calculates the remainder of the division operation using the modulus operator %. The modulus operator returns the remainder when the dividend is divided by the divisor.

The following lines print the calculated quotient and remainder:

System.out.println("Quotient is: " + quotient);
System.out.print("Remainder is: " + remainder);

The System.out.println() and System.out.print() statements display the quotient and remainder values, respectively. The values are concatenated with other strings to form the output messages.