Table of Contents
In this tutorial, we will discuss the Find division of two numbers in Java language
In this post, we are going to learn how to find division of two numbers via different 5 ways in Java.
Program 1
public class FindDivision{ public static void main(String args[]){ int num1=120,num2=15; int div=num1/num2; System.out.println("Division of "+num1+" and "+num2+":"+div); } }
When the above code is executed, it produces the following result
division of 120 and 15 is: 8
In this program,
Program 2
import java.util.Scanner; public class FindDivision1{ public static void main(String args[]){ 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 div=num1/num2; System.out.println("Division of "+num1+" and "+num2+":"+div); } }
When the above code is executed, it produces the following result
Enter the value to num1: 1000 Enter the value to num2: 25 Division of 1000 and 25: 40
In the program,
Program 3
import java.util.Scanner; public class FindDivision2{ static void divisionNum(int x,int y){//user defind method int division=x/y;//caculate Division of two numbers System.out.println("Division of "+x+" and "+y+":"+division); //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(); divisionNum(num1,num2);//calling the method } }
When the above code is executed, it produces the following result
Enter the value to num1: 2400 Enter the value to num2: 80 Division of 1000 and 25: 30
In the program,
Program 4
import java.util.Scanner; public class FindDivision3{ 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 result=divisionNum(num1,num2);//calling the method System.out.println("Division of "+num1+" and "+num2+":"+result); } static int divisionNum(int x,int y){//user defind method if (y==0) { return 0; } else if (x-y==0){ return 1; } else if (x<y){ return 0; } else{ return (1+divisionNum(x-y,y)); } } }
When the above code is executed, it produces the following result
Enter the value to num1: 240 Enter the value to num2: 16 Division of 240 and 16: 15
In the program,
Program 5
import java.util.Scanner; class Divwithoutdivision{ public static void main(String args[]){//main method int num1,num2,div,result=0; 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: "); num1=scan.nextInt(); System.out.print("Enter the value to num2: "); num2=scan.nextInt(); div=num1+num2; while(div>num2){ div=div-num2; result++; } System.out.println("Division of "+num1+" and "+num2+":"+result); } }
When the above code is executed, it produces the following result
Enter the value to num1: 200
Enter the value to num2: 10
Division of 200 and 10: 20
Suggested post
Data type and variable in Java language
variable types in Java language
Recursion in Java language
Similar post
C program to calculate division of two numbers | 6 different Methods
C++ program to calculate division of two numbers | 6 different Methods
Python program to calculate division of two numbers | 4 different Methods
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.