Signup/Sign In
PUBLISHED ON: MARCH 11, 2021

Java Program to find Quotient and Remainder

In this tutorial, we will learn how to find the quotient and remainder by taking input from the user. But before moving further, if you are not familiar with the concept of the arithmetic operator in java, then do check the article on Operators in Java.

Input: Enter the first number: 6

Enter the second number: 2

Output:

The quotient of 6 and 2 is 3

The remainder of 6 and 2 is 0

The above problem can be solved in the following ways:

Approach 1: When the values are pre-defined

Approach 2: When the values are user-defined

Let us look at each of these approaches separately.

Program 1:To Find the Quotient and Remainder

In this program, we will find the quotient and remainder of two numbers when the numbers are user-defined.

Algorithm:

  1. Start
  2. Declare two variables.
  3. Initialize the variables.
  4. Use the division operator to find the quotient.
  5. Use the modulo operator to find the remainder.
  6. Display the quotient and remainder.
  7. Stop.

Below is the code for the same.

//Java Program to find the quotient and remainder
public class Main 
{
    public static void main(String[] args) 
    {
        int num1 = 19, num2 = 4;  //Declare and initialize the numbers
        System.out.println("The entered number is: "+num1);
        System.out.println("The entered number is: "+num1);
        int quotient = num1 / num2;   //Find quotient
        int remainder = num1 % num2;  //Find Remainnder
        System.out.println("After division the quotient and remainder are: ");
        //Print the quotient and remainder
        System.out.println("The quotient is: " + quotient);
        System.out.println("The remainder is: " + remainder);
    }
}


The entered number is: 19
The entered number is: 19
After division the quotient and remainder are:
The quotient is: 4
The remainder is: 3

Program 2: To Find the Quotient and Remainder

In this program, we will find the quotient and remainder of two numbers when the numbers are user-defined. This means, here first we will ask the user to initialize the numbers and then we will find the quotient and remainder.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class to take input from the user.
  3. Declare two variables.
  4. Ask the user to initialize it.
  5. Use the division operator to find the quotient.
  6. Use the modulo operator to find the remainder.
  7. Display the quotient and remainder.
  8. Stop.

Below is the code for the same.

//Java Program to find the quotient and remainder
import java.util.*;
public class Main 
{
    public static void main(String[] args) 
    {
        //Take input from the user
        //Create object of Scanner class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the numbers ");
        System.out.println("Enter the first number: ");
        int num1=sc.nextInt();  //Initialize the number
        System.out.println("Enter the second number: ");
        int num2=sc.nextInt();  //Initialize the number
        int quotient = num1 / num2;
        int remainder = num1 % num2;
        System.out.println("After division the quotient and remainder are:");
        //Print the Quotient 
        System.out.println("The quotient is: " + quotient);
        System.out.println("The remainder is: " + remainder);
    }
}


Enter the numbers
Enter the first number: 19
Enter the second number: 7
After division the quotient and remainder are:
The quotient is: 2
The remainder is: 5

Program 3: To Find the Quotient and Remainder

In this program, we will use a user-defined method to find the quotient and remainder with user-defined inputs.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class to take input from the user.
  3. Declare two variables.
  4. Ask the user to initialize it.
  5. Call a user-defined method to find the quotient and remainder.
  6. Use the division operator to find the quotient.
  7. Use the modulo operator to find the remainder.
  8. Display the quotient and remainder.
  9. Stop.

Below is the code for the same.

//Java Program to find the quotient and remainder
import java.util.*;
public class Main 
{
    public static void main(String[] args) 
    {
        //Take input from the user
        //Create object of Scanner class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the numbers ");
        System.out.println("Enter the first number: ");
        int num1=sc.nextInt();  //Initialize the number
        System.out.println("Enter the second number: ");
        int num2=sc.nextInt();  //Initialize the number
        findQuotient(num1,num2);
    }
    //user defined method
    static void findQuotient(int num1, int num2)
    {
       int quotient=num1/num2;
       int remainder=num1%num2;
       
       //display result
       System.out.println("The quotient of "+num1+" and "+num2+" is "+quotient);
       System.out.println("The remainder of "+num1+" and "+num2+" is"+remainder);
      
    }
}


Enter the numbers
Enter the first number: 9
Enter the second number: 7
The quotient of 9 and 7 is 1
The remainder of 9 and 7 is 2



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.