Table of Contents
In this article, we will discuss the concept of How to Divide two numbers in Java| 5 different ways
In this post, we are going to learn how to write a program to 5 Ways to Division of Two Numbers (With Examples) in Java programming language.
here, I’ll give you separate short programs for each method so it’s easier to understand
The simplest and most common way to divide two numbers
Program 1
// Divide two nunbers using simple division operator
class DivideNum {
public static void main(String[] args) {
int num1=24;
int num2=4;
int result=num1/num2;
System.out.println("Division using / operator ");
System.out.println("Division is: "+result);
}
} When the above code is executed, it produces the following result
Division using / operator Division is: 6
Division of two numbers Using Allows users to enter numbers at runtime.
Program 2
// Divide two nunbers using Scanner Class
import java.util.Scanner;
class DivideNum2 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter first number ");
double num1=input.nextDouble();
System.out.println("Enter second number ");
double num2=input.nextDouble();
double result=num1/num2;
System.out.println("Division using Scanner ");
System.out.println("Division is: "+result);
}
} When the above code is executed, it produces the following result
Enter first number 33 Enter second number 11 Division using Scanner Division is: 3.0
Division is done inside a method for reusability.
Program 3
// Divide two nunbers using user defined-method
class DivideNum2 {
public static double divide(double a, double b){
if(b==0){
System.out.println("Cannot divide by zero ");
return 0;
}
return a/b;
}
public static void main(String[] args) {
double result=divide(25,5);
System.out.println("Division using user difined method ");
System.out.println("Division is: "+result);
}
} When the above code is executed, it produces the following result
Division using user defined method Division is: 5.0
performs division without using the / operator – good for understanding logic
Program 4
// Divide two numbers using while loop
class DivideNumbers {
public static void main(String[] args) {
int dividend=30;
int divisor=4;
int quotient=0;
while(dividend>= divisor){
dividend-=divisor;
quotient++;
}
System.out.println("Quotient using loop: "+quotient);
System.out.println("Remainder: "+dividend);
}
} When the above code is executed, it produces the following result
Quotient using loop: 7 Remainder: 2
Program 5
// Divide two numbers using recursion
class DivideNum2 {
// Method to divide two numbers
public static int divide(int dividend, int divisor){
if(divisor==0){
throw new ArithmeticException("Cannot divide by zero ");
}
if(dividend<divisor){
return 0;
}
return 1+divide(dividend - divisor,divisor);
}
public static void main(String[] args) {
System.out.println("Division using user difined method ");
// Calling the divide method
double result = divide(20,4);
// Displaying the result
System.out.println("Division result: " + result);
}
} When the above code is executed, it produces the following result
Division using user defined method Division result: 5.0
In the above program, the program defines a recursive function add(a,b).
The recursion gradually increases a and decreases b until the total sum is reached.
Similar post
Java program to division of two numbers – 5 ways
PHP program to division of two numbers
C# program to divide two numbers
C++ program to divide two numbers – 6 ways
JavaScript program to divide two numbers – 4 ways
Java code two divide two numbers using method
Java program to division of two integer numbers
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
5 Ways to Add Two Numbers in Java with User Input (Examples) In this article,…
This website uses cookies.