Arithmetic Caculation

Find division of two numbers in Java language|5ways

Find division of two numbers in Java language|5ways

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.

Division of two numbers in Java language

Find division of two numbers in Java – standard method

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,

  • integer variables num1,num2 are declared and initialized
  • The num1 and num2 both are divided together and the value is assigned to the variable div.
  • Then,the program is displayed the output of using System.out.println();

Find division of two numbers in Java – using user input

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,

  • 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 divided together and the value assigned to the div variable
  • Then, the program displays the output using System.out.println() function

 

Find division of two numbers in Java – using method

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,

  • Declare variables num1, num2, division.
  • 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(divisionNum()) to caculate division of given two numbers
  • Call the method to produce output
  • finally, the program displays the value of  division using System.out.println()

 

Find division of two numbers in Java – using recursion

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,

  • Declare variables num1, num2 and result.
  • 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(divisionNum()) to caculate division recursively
  • Call the recursive method to produce output
  • finally, the program displays the value of division using System.out.println().

 

 

Find division of two numbers in Java – using without divisional operator

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

Operator in Java language

if statement in Java language

For loop in Java language

Method 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

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.