Division in Java:
This article will show you the process to divide in Java with output.
The division is one of the basic operations in java. Others include addition, subtraction, and multiplication. For dividing in java, a division operator (/)is used. This operator takes two operands(the natural numbers to be divided) to divide them.
We will see two ways for java division:
- First by declaring the number to be divided inside the program.
- Second by taking the user input.
1. Java Program to divide two numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class DivisionJava { public static void main(String[] args) { //declaring operands float num1 = 70, num2 = 3; //division float result = num1/num2; //Display System.out.println("The result is: "+result); } } |
The output of the division in java:
1 | The result is: 23.333334 |
2. Java Program to Divide two Numbers taking User’s Input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.util.Scanner; public class JavaDivision { public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.print("Enter the first number (Denominator): "); int num1 = input.nextInt(); System.out.print("Enter the second number (Numerator): "); int num2 = input.nextInt(); //Division int result = (num1/num2); System.out.println("\nThe result of division is:" +result); } } |
The output of the user input division in Java:
1 2 3 4 | Enter the first number (Denominator): 70 Enter the second number (Numerator): 3 The result of division is:23.333334 |