Table of Contents
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.
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,
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
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,
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.