codescracker
java

Java Program to Find Quotient and Remainder



« Java Tutorial Java Examples »

This article is created to cover a program in Java that calculates the quotient and remainder value while dividing a number with another.

Before creating the program, let's remind all the terms such as Divisor, Quotient, Dividend, Remainder, using following figure:

java divisor dividend quotient remainder

Here the Dividend can also be referred as Numerator, and Divisor can also be referred as Denominator.

Find Quotient and Remainder in Java

The question is, write a Java program to find and print the quotient and the remainder value. The data say dividend and divisor must be received by user at run-time. The program given below is answer to this question:

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String args[])
   {
      float numerator, denominator;
      int quotient, remainder;
      Scanner s = new Scanner(System.in);
      
      System.out.print("Enter the Dividend: ");
      numerator = s.nextFloat();
      System.out.print("Enter the Divisor: ");
      denominator = s.nextFloat();
      
      quotient = (int) (numerator/denominator);
      remainder = (int) (numerator%denominator);
      
      System.out.println("\nQuotient = " +quotient);
      System.out.println("Remainder = " +remainder);
   }
}

Here is its sample run with user input 21 as dividend and 8 as divisor:

java compute quotient remainder

When dividing the number 21 by 8, we will get 2 as quotient and 5 as remainder. That is, 8 (divisor) * 2 (quotient) + 5 (remainder) = 21 (dividend).

Java Online Test


« Java Tutorial Java Examples »



© Copyright 2021. All Rights Reserved.

CodesCracker