1. Introduction
Creating a Java program to divide two numbers is an excellent step for beginners to familiarize themselves with various programming concepts such as taking user input, performing arithmetic operations, handling exceptions, and displaying output.
2. Program Steps
1. Import the Scanner class from the java.util package.
2. Define the main class named DivideTwoNumbers.
3. Inside the main class, define the main method.
4. Inside the main method, create an object of the Scanner class to take user input.
5. Prompt the user to enter the dividend and store it in a variable.
6. Prompt the user to enter the divisor and store it in another variable.
7. Check if the divisor is zero and handle the exception if true.
8. If the divisor is not zero, divide the dividend by the divisor and store the result in a third variable.
9. Print the result to the console.
3. Code Program
import java.util.Scanner; // 1. Importing Scanner class for user input
public class DivideTwoNumbers { // 2. Defining the main class
public static void main(String[] args) { // 3. Defining the main method
Scanner input = new Scanner(System.in); // 4. Creating Scanner object to take user input
System.out.print("Enter the dividend: "); // 5. Prompting user for the dividend
double dividend = input.nextDouble(); // Storing the dividend in variable dividend
System.out.print("Enter the divisor: "); // 6. Prompting user for the divisor
double divisor = input.nextDouble(); // Storing the divisor in variable divisor
// 7. Checking if divisor is zero
if (divisor == 0) {
System.out.println("Error! Dividing by zero is not allowed.");
} else {
double quotient = dividend / divisor; // 8. Dividing dividend by divisor, storing the result in variable quotient
System.out.println("The quotient of " + dividend + " and " + divisor + " is: " + quotient); // 9. Printing the result
}
input.close(); // Closing the Scanner object to avoid memory leak
}
}
Output:
Enter the dividend: 20 Enter the divisor: 4 The quotient of 20.0 and 4.0 is: 5.0
4. Step By Step Explanation
– Step 1: The Scanner class from the java.util package is imported, which facilitates user input.
– Step 2: The main class DivideTwoNumbers is defined.
– Step 3: Inside the main class, the main method is defined as the entry point of the Java program.
– Step 4: A Scanner object named "input" is created to read the user's input.
– Step 5: The user is prompted to enter the dividend, 20, which is then stored in the variable dividend.
– Step 6: The user is also prompted to enter the divisor, 4, which is stored in the variable divisor.
– Step 7: Before performing the division, the program checks if the divisor is zero. If it is, an error message is displayed, preventing division by zero.
– Step 8: If the divisor is not zero, the dividend is divided by the divisor, and the result, 5.0, is stored in a new variable named quotient.
– Step 9: Finally, the quotient of the division is printed to the console.
– The Scanner object is then closed to prevent resource leak.