Table of Contents
In this article, we will discuss the concept of Java Code Examples – 5 Different ways to Divide two numbers in Java using scanner
In this post, we are going to learn how to write a program to 10 Ways to Subtract Two Numbers (With Examples) in the Java programming language.
here, I’ll give you separate short programs for each method so it’s easier to understand
Divide two numbers using divisional (/) operator with user input. The simplest and most common way to divide two numbers
Program 1
// Division of two numbers in Java
// Basic Division using / operator
import java.util.Scanner;
class Basic_Division{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Input first number: ");
int num_1=sc.nextInt();
System.out.print("Input second number: ");
int num_2=sc.nextInt();
System.out.println("Division of two numbers: "+(num_1/num_2));
}
} When the above code is executed, it produces the following result
Input first number: 100 Input second number: 5 Division of two numbers: 20
Divide two numbers using divisional (/) operator
Program 2
// Divide two numbers using while loop with user input
import java.util.Scanner;
class DivideNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Taking user input
System.out.print("Enter the dividend: ");
int dividend = input.nextInt();
System.out.print("Enter the divisor: ");
int divisor = input.nextInt();
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
Enter the dividend: 45 Enter the divisor: 9 Quotient using loop: 5 Remainder: 0
Divide two numbers using method with user input
Program 3
// Divide two numbers using method with user input
import java.util.Scanner;
class DivideNum2 {
// Method to divide two numbers
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) {
Scanner sc = new Scanner(System.in);
System.out.println("Division using user difined method ");
// Taking user input
System.out.print("Enter the dividend: ");
double dividend = sc.nextDouble();
System.out.print("Enter the divisor: ");
double divisor = sc.nextDouble();
// Calling the divide method
double result = divide(dividend, divisor);
// Displaying the result
System.out.println("Division result: " + result);
System.out.println("Division is: "+result);
}
}
When the above code is executed, it produces the following result
Division using user defined method Enter the dividend: 56 Enter the divisor: 7 Division result: 8.0 Division is: 8.0
Program 4
Divide two numbers Using recursion with user input
// Divide two numbers using recursion with user input
import java.util.Scanner;
class divideRecursionInput{
//Recursive method to perform integer division
public static int divide(int dividend, int divisor){
if(dividend<divisor) return 0;
return 1+divide (dividend-divisor,divisor);
}
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter dividend: ");
int dividend=input.nextInt();
System.out.println("Enter divisor: ");
int divisor=input.nextInt();
System.out.println("Quotient:" +divide(dividend,divisor));
}
} When the above code is executed, it produces the following result
Enter dividend: 35 Enter divisor: 7 Quotient:5
Program 5
Divide two numbers Using for loop(Repeated Subtraction)
// Divide two numbers using for loop with user input
import java.util.Scanner;
class divideForInput{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter dividend: ");
int dividend=input.nextInt();
System.out.println("Enter divisor: ");
int divisor=input.nextInt();
int quotient=0;
for(; dividend>=divisor; dividend-=divisor){
quotient++;
}
System.out.println("Quotient:" +quotient);
System.out.println("Remainder:" +dividend);
}
} When the above code is executed, it produces the following result
Enter dividend: 60 Enter divisor: 8 Quotient:7 Remainder:4
above programs Allow users to enter numbers at runtime.
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
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
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…
How to Divide two numbers in Java| 5 different ways In this article, we will…
5 Ways to Add Two Numbers in Java with User Input (Examples) In this article,…
This website uses cookies.