Table of Contents
Java Program to Compute Quotient and Remainder
In this tutorial, we will discuss the Java Program to Compute Quotient and Remainder
In this post, we are going to learn how to find quotient and remainder of two numbers using different 5 ways in Java.
Compute Quotient and Remainder
Compute Quotient and Remainder-standard method
Program 1
public class FindQuotient{ public static void main(String args[]){//main method int num1=45,num2=4; int quotient=num1/num2; // / symbol is used to calculate quotient int remainder=num1%num2; // % symbol is used to calculate remainder System.out.println("Quotient is: "+quotient); System.out.println("remainder is: "+remainder); } }
When the above code is executed, it produces the following result
Quotient is: 11 remainder is: 1
In this program,
- integer variables num1,num2 are declared and initialized
- The num1 and num2 both are used to calculate quotient and remainder
- Then,the program is displayed the quotient and remainder of using System.out.println().
Compute Quotient and Remainder-using user input
Program 2
import java.util.Scanner; public class FindQuotient1{ public static void main(String args[]){//main method Scanner scan=new Scanner(System.in); //create a scanner instance for receives input // from the user - input from keyboard System.out.print("Enter the value to num1: "); int num1=scan.nextInt(); System.out.print("Enter the value to num2: "); int num2=scan.nextInt(); int quotient=num1/num2; // / symbol is used to calculate quotient int remainder=num1%num2; // % symbol is used to calculate remainder System.out.println("Quotient is: "+quotient); System.out.println("remainder is: "+remainder); } }
When the above code is executed, it produces the following result
Enter the value to num1: 555 Enter the value to num1: 20 Quotient is: 27 remainder is: 15
- Declare variables num1, num2
- The program asks input from the user
- Then the user enters the input values for num1, num2.
- The program will read the input using scanner class and store the variables num1 and num2 respectively
- The num1 and num2 both are used to calculate quotient and remainder
- Then, the program displays the quotient and remainder using System.out.println() function
Compute Quotient and Remainder-using method
Program 3
import java.util.Scanner; public class FindQuotient2{ static void quotientNum(int x,int y){//user defind method int quotient=x/y;//calculate quotient of two numbers int remainder=x%y;//calculate remainder of two numbers System.out.println("Quotient of "+x+" and "+y+":"+quotient); System.out.println("remainder of "+x+" and "+y+":"+remainder); //display result } public static void main(String args[]){//main method Scanner scan=new Scanner(System.in); //create a scanner instance for receives input // from the user - input from keyboard System.out.print("Enter the value to num1: "); int num1=scan.nextInt(); System.out.print("Enter the value to num2: "); int num2=scan.nextInt(); quotientNum(num1,num2);//calling the method } }
When the above code is executed, it produces the following result
Enter the value to num1: 345 Enter the value to num1: 20 Quotient of 345 and 20: 17 remainder of 345 and 20: 5
In the program,
- Declare variables num1, num2.
- The program asks input from the user
- Then the user enters the input values for num1, num2.
- The program will read the input using scanner class and store the variables num1 and num2 respectively
- Define the method(quotientNum()) to calculate division of given two numbers
- Call the method to produce output
- finally, the program displays quotient and remainder using System.out.println()
Program 4
import java.util.Scanner; public class FindQuotientMethodreturn{ static int quotientNum(int x,int y){//user defind method for find quotient int quotient=x/y;//caculate quotient of two numbers return quotient; //Using return keyword to return result to main method } static int remainderNum(int x,int y){//user defind method for find remainder int remainder=x%y;//caculate remainder of two numbers return remainder; //Using return keyword to return result to main method } public static void main(String args[]){//main method Scanner scan=new Scanner(System.in); //create a scanner instance for receives input // from the user - input from keyboard System.out.print("Enter the value to num1: "); int num1=scan.nextInt(); //reading the user input for num1 System.out.print("Enter the value to num2: "); int num2=scan.nextInt(); //reading the user input for num2 System.out.println("Quotient of "+num1+" and "+num2+":"+quotientNum(num1,num2)); //calling the method to display quotient System.out.println("remainder of "+num1+" and "+num2+":"+remainderNum(num1, num2)); //calling the method to display remainder } }
Enter the integer value to num1: 56
Enter the integer value to num2: 5
Quotient of 56 and 5 is: 11
Remainder of 56 and 5 is: 1
Suggested post
Data type and variable in Java language
variable types in Java language
Recursion in Java language
Similar post
Python Program to Compute Quotient and Remainder