How to find remainder in java – Java Program to find Quotient and Remainder of a Number

How to find remainder in java: In the previous article, we have seen Java Program to Generate Multiplication Table of a Number

In this article we will see how to find quotient and remainder of a number by using Java programming language.

Java Program to find Quotient and Remainder of a Number

How to get the remainder in java: Before jumping into the program directly let’s know how we can get remainder and quotient of a number.

To get the remainder of a number:

Dividend % Divisor = Remainder

To get quotient of a number:

Dividend / Divisor = Quotient

Where,

  • Dividend: The number which will be divided
  • Divisor: The number which will divide
  • %: Modulo Operator
  • /: Division Operator

Now, let’s see the program to understand it more clearly.

Approach:

  1. Create Scanner class object.
  2. Take user input for the dividend and the divisor.
  3. Use ‘/’ operator obtain the quotient.
  4. Use ‘%’ operator obtain the remainder.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        //creating scanner class object
        Scanner sc = new Scanner(System.in);
        //taking input from user
        System.out.print("Enter Dividend: ");
        int dividend = sc.nextInt();
        System.out.print("Enter Divisor: ");
        int divisor = sc.nextInt();
        //calling method to print quotient and remainder
        printQuotientAndRemainder(dividend, divisor);
    }
    
    private static void printQuotientAndRemainder(int dividend, int divisor) 
    {
        //printing quotient and remainder
        System.out.println("Quotient: " + dividend / divisor);
        System.out.println("Remainder: " + dividend % divisor);
    }
}

Output:

Enter Dividend: 567
Enter Divisor: 11
Quotient: 51
Remainder: 6

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Programs: