# Remainder calculator java – Java Program to Compute Quotient and Remainder

Remainder calculator java: Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

## Program to Compute Quotient and Remainder

How to get remainder in java: In this article we will see multiple ways to compute Quotient and Remainder in Java. Before going into the actual concept let’s first understand this quotient and remainder then we will move to the actual part.

Remainder: It can be defined as the left over remains after dividing two numbers.

Quotient: It is the amount we get after dividing two numbers.

We know that we can calculate quotient by dividing, dividend by divisor using '/'  operator. And we can calculate remainder using '%' operator between dividend and divisor.

We will use following expressions to calculate quotient and remainder.

1. quotient = dividend / divisor;
2. remainder = dividend % divisor;

Now our aim is to find quotient and remainder of two numbers. We can use different approaches to find it.

Let’s see one by one.

### Method-1 : Computing Quotient and Remainder by using ‘/’ and ‘%’ operator.

Remainder in java: With given dividend and divisor,  we can compute quotient and remainder. We can implement this using below approach.

Approach:

• Declare and initialize any two variables.
• Then use ‘/’ operator between dividend and divisor to find the quotient.
• Then use ‘%’ operator between dividend and divisor to find the remainder.
• Now, let’s write a program using following approach.
import java.util.*;

public class ComputeQuotientandRemainder
{

public static void main(String[] args)
{
//Take dividend as divisor as variables dvnd and dvsr respectively
int dvnd = 458, dvsr = 8;
// Take quotient as variable quot and compute quotient
// Store the result in quot
int quot = dvnd / dvsr;
// Take remainder as variable rem and compute remainder
// Store the result in rem
int rem = dvnd % dvsr;
// Print the quotient value
System.out.println("Quotient = " + quot);
// Print the remainder value
System.out.println("Remainder = " + rem);
}
}

Output:

Quotient = 57
Remainder = 2

### Method-2 : Compute Quotient and Remainder where Dividend is a negative number

How to use remainder in java: We can calculate quotient and remainder of a number where dividend is given as a negative number using below approach.

Approach

• Declare and initialize any two variables. Say dvnd for dividend and dvsr for divisor.
• Calculate quotient by dividing dvnd and dvsr using ‘/’ operator i.e dvnd/dvsr. It will provide the quotient, assign this value to another variable, say quot.
• Calculate remainder by using ‘%’ operator between dividend and divisor i.e dvnd%dvsr. It will provide the remainder, assign this value to another variable, say rem.
• Print rem and res. Now, let’s write a program using following approach.
import java.util.*;

public class ComputeQuotientandRemainder
{

public static void main(String[] args)
{
//Take dividend as divisor as variables dvnd and dvsr respectively
int dvnd = -367, dvsr = 9;
// Take quotient as variable quot and compute quotient
// Store the result in quot
int quot = dvnd / dvsr;
// Take remainder as variable rem and compute remainder
// Store the result in rem
int rem = dvnd % dvsr;
// Print the quotient value
System.out.println("Quotient = " + quot);
// Print the remainder value
System.out.println("Remainder = " + rem);
}
}

Output:

Quotient = -40
Remainder = -7

### Method-3 :- Compute Quotient and Remainder taking user defined inputs

In Java, java.util package provide a class i.e. Java Scanner class through which we can ask user to enter the inputs.

Then we can store the input in variables and calculate quotient and remainder. Let’s try to implement it using below approach.

Approach

• Input dividend and divisor from user and assign to say dvnd and dvsr variables respectively.
• Calculate quotient by dividing dvnd and dvsr using ‘/’ operator i.e dvnd/dvsr. It will provide the quotient, assign this value to another variable, say quot.
• Calculate remainder by using ‘%’ operator between dividend and divisor i.e. dvnd%dvsr. It will provide the remainder, assign this value to another variable, say rem.
• Print rem and res. Now, let’s write a program using following approach.
import java.util.Scanner;
// Import Scanner class of java.util package
public class ComputeQuotientandRemainder {

public static void main(String[] args)
{
// declare required variables to be used
int dvnd, dvsr;
// create a Scanner class object
Scanner sc= new Scanner(System.in);

System.out.println("Enter dividend value: ");
// Take dividend value from user
dvnd=sc.nextInt();

System.out.println("Enter divisor value: ");
// Take divisor value from user
dvsr=sc.nextInt();

// Take quotient as variable quot and compute quotient
// Store the result in quot
int quot = dvnd / dvsr;
// Take remainder as variable rem and compute quotient
// Store the result in rem
int rem = dvnd % dvsr;
// Print the quotient value
System.out.println("Quotient = " + quot);
// Print the remainder value
System.out.println("Remainder = " + rem);
}
}

Output:

Enter dividend value:
458
Enter divisor value:
8
Quotient = 57
Remainder = 2

### Method-4 : Compute Quotient and Remainder without using ‘/’ and ‘%’ operator

We can also calculate quotient by repeatedly subtracting the divisor from dividend till it becomes less than divisor. The total number of times repeated subtraction is carried out is same as the quotient.

The left out value we get after repeated subtraction is the remainder.

Here we can also solve without taking user defined method, let’s try to implement taking a user defined method say Divide. Then we would pass arguments of dividend and divisor through main function to the Divide method. Here the Divide function will compute quotient and remainder.

java.lang package provide abs method under Math class i.e. Math.abs() that returns Absolute or positive value of a argument.

1. If given argument is not negative, simply the argument is returned.
2. If given argument is negative, it will return negation of argument.

Let’s implement this using below approach.

Approach

• Declare and initialize any two variables. Say dvnd for dividend and dvsr for divisor. Initialize variable say quot as 0 which will be used to calculate quotient.
• Pass this as a argument to a method say Div.
• Take a loop as while (dvnd>=dvsr). And if the condition satisfies, it do
1. dividend= dividend-divisor and
2. increment variable quot
• After coming out of the while loop dvnd will contain value of remainder. And the quot value will serve as quotient. Now, let’s write a program using following approach.
import java.util.*;

class ComputeQuotientandRemainder
{
// value of dvnd and dvsr passed to a and b respectively
public static int div(int dvnd, int dvsr)
{
//store the sign of the result
int sign = 1;
if (dvnd * dvsr < 0) {
sign = -1;
}
// convert dividend & divisor to positive value
dvnd = Math.abs(dvnd);
dvsr = Math.abs(dvsr);

// initialize quotient as quot with 0
int quot = 0;

// iterate till x i.e. dvnd  is greater than y i.e. dvsr
while (dvnd >= dvsr)
{
dvnd = dvnd - dvsr;      // dvnd decremented by value of dvsr
quot++;     // Increment quot with each iteration
}

//Left out value after repeated division is the remainder
// Print the remainder
System.out.println("Remainder = " + dvnd);
// Return the quotient to main function
// sign is assigned to quotient
return sign*quot;
}

public static void main(String[] args)
{
int dvnd = 458; // Declare Dividend
int dvsr = 8; // Declare Divisor
// Print the quotient
System.out.println("Quotient = " + div(dvnd, dvsr));
}
}


Output:

Remainder = 2
Quotient = 57

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Basic Programs: