This Java code example demonstrates how to compute the quotient and remainder when dividing two integers. It takes user input for the dividend and divisor, performs the division, and then prints the quotient and remainder. This example highlights the use of input handling, basic arithmetic operations, and formatted output in Java.
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.println("Remainder is: " + remainder);
}
}
public static void main(String[] args)
The main method executes the program and contains the logic for calculating the quotient and remainder.
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.println("Remainder is: " + remainder);
}
Scanner reader = new Scanner(System.in);
creates a Scanner
object to read input from the user.int dividend, divisor, quotient, remainder;
declares variables to store the dividend, divisor, quotient, and remainder.System.out.print("Please enter dividend: ");
prompts the user to enter the dividend.dividend = reader.nextInt();
reads the integer input for the dividend.System.out.print("Please enter divisor: ");
prompts the user to enter the divisor.divisor = reader.nextInt();
reads the integer input for the divisor.reader.close();
closes the Scanner
object to free up resources.quotient = dividend / divisor;
calculates the quotient by dividing the dividend by the divisor.remainder = dividend % divisor;
calculates the remainder using the modulus operator.System.out.println("Quotient is: " + quotient);
prints the calculated quotient.System.out.println("Remainder is: " + remainder);
prints the calculated remainder.