In this tutorial you will learn about the Java Program to find quotient and remainder and its application with practical example.
In this tutorial, we will learn to create a Java Program to find Quotient and Remainder in Java using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
Quotient and Remainder
For find the Quotient:
we will use “/” operator.
For that we must divided by divisor “dividend/divisor”.
Example:=> 12/4 =3 gives 3
For find the Remainder:
Same as to find the remainder we use “%”operator.
the dividend is divided by the divisor and the remainder returned by the “%” modules operator.
Example:=> 12%4 =0 gives 0.
Java Program to find quotient and remainder
In this program we will learn to create a Program to find Quotient and Remainder .We would first declared and initialized the required variables. Next, we would prompt user to input dividend and Divisor . Later in program we will find quotient and remainder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// java program to calculate quotient and Remainder. import java.util.Scanner; public class QuotientRemainder { public static void main(String[] args) { // Declaring variables and takin value from user.. int number,divsr,quotient,remainder; Scanner s = new Scanner(System.in); System.out.println("please enter dividend / number"); dividend = s.nextInt(); Scanner sc = new Scanner(System.in); System.out.println("please enter divisor "); divsr = sc.nextInt(); // Finding Quotient.. quotient = number / divsr; // Finding Remainder.. remainder = number % divsr; // Printing the result for Quotient.. System.out.println("Quotient = " + quotient); // Printing the result for Remainder.. System.out.println("Remainder = " + remainder); } } |
Output
In the above program, we have first declared and initialized a set variables required in the program.
- number= it will hold Dividend
- divsr= it will hold divisor.
- quotient= it will hold Quotient value.
- remainder= it will hold Remainder.
After declaring variables we going to take value of Dividend and Divisor form user as shown in picture below.
After taking all the values,Now we will calculate Both Quotient and Remainder
First of all we calculate the Quotient for finding quotient we simply divide dividend by divisor using “/” operator.
then we calculate the Remainder for finding remainder we simply divide dividend by divisor using “%” operator.
At last will print the return value by performing the operations as shown below.