C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Introduction to Java

Find Quotient & Remainder - Java Program

import java.util.Scanner;

 

public class QuotientRemainder {

    public static void main(String[] args) {

 

        Scanner sc = new Scanner(System.in);

 

        // Taking input from the user

        System.out.print("Enter dividend: ");

        int dividend = sc.nextInt();

 

        System.out.print("Enter divisor: ");

        int divisor = sc.nextInt();

 

        // Checking for division by zero

        if (divisor == 0) {

            System.out.println("Error: Divisor cannot be zero!");

            sc.close();

            return;

        }

 

        // Calculating quotient and remainder

        int quotient = dividend / divisor;

        int remainder = dividend % divisor;

 

        // Displaying the result

        System.out.println("\nQuotient = " + quotient);

        System.out.println("Remainder = " + remainder);

 

        sc.close();

    }

}

Output

 
OUTPUT 1:
Enter dividend: 43
Enter divisor: 5

Quotient = 8
Remainder = 3

Explanation

Dividend

The number to be divided (example: 43)

Divisor

The number that divides the dividend (example: 5)

Quotient Calculation

The quotient is the result of integer division:

43 / 5 = 8

It does not include decimal part.

Remainder Calculation

The remainder is the value left after division:

43 % 5 = 3

The % operator returns the remainder.

Division by Zero Handling

Division by zero is not allowed, so the program checks:

if (divisor == 0)

If true, it stops the program with an error message.