Add Two numbers user Input Java Program

Java Program add Two numbers user Input
Reading Time: 7 minutes

Introduction :

The provided Java program, named “AddTwoNumbers,” is a simple console application that takes user input for two numbers and calculates their sum. It utilizes the Scanner class to read input from the user and performs basic arithmetic operations to compute the sum.

Algorithm :

  1. Import the Scanner class from the java.util package to facilitate user input.
  2. Create a class named “AddTwoNumbers” to encapsulate the program logic.
  3. Inside the main method: a. Initialize a Scanner object (scanner) to read input from the console. b. Prompt the user to enter the first number and store it in the variable “num1.” c. Prompt the user to enter the second number and store it in the variable “num2.” d. Calculate the sum of the two numbers and store it in the variable “sum.” e. Display the result by printing a message containing the input numbers and their sum. f. Close the Scanner to release system resources.

Explanation :

  • The program begins by importing the Scanner class, which is essential for obtaining user input.
  • The main method is the entry point of the program, where the actual computation and interaction take place.
  • A Scanner object named “scanner” is created to read input from the user.
  • The user is prompted to enter the first and second numbers using the “System.out.print” statements.
  • The entered numbers are then read using the “scanner.nextDouble()” method and stored in the variables “num1” and “num2.”
  • The sum of the two numbers is calculated and stored in the variable “sum” using the addition operator.
  • The program then prints a message to the console, displaying the input numbers and their sum.
  • Finally, the Scanner is closed using the “scanner.close()” method to prevent resource leaks.

This program serves as a basic example of user input, arithmetic operations, and output in Java, making it suitable for educational purposes or as a starting point for more complex applications.

Code :

				
					import java.util.Scanner;
public class AddTwoNumbers {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // Get user input for the first number
        System.out.print("Enter the first number: ");
        double num1 = scanner.nextDouble();
        // Get user input for the second number
        System.out.print("Enter the second number: ");
        double num2 = scanner.nextDouble();
        // Add the two numbers
        double sum = num1 + num2;
        // Display the result
        System.out.println("Sum of " + num1 + " and " + num2 + " is: " + sum);
        // Close the scanner
        scanner.close();
    }
}

				
			

Input :

				
					Enter the first number: 569
Enter the second number: 965
				
			

Output :

				
					Sum of 569.0 and 965.0 is: 1534.0
				
			

Copy and paste this code into a Java file (e.g., AddTwoNumbers.java) and then compile and run it using a Java compiler.

FAQs :

  1. How does the Java program take user input for two numbers?
    • The program uses the Scanner class from the java.util package to read input from the console. It prompts the user to enter the first and second numbers, and then uses the nextDouble() method to capture the user input.
  2. What happens if a user enters a non-numeric value as input?
    • If a user enters a non-numeric value (e.g., a letter or a symbol), the program will throw an InputMismatchException. It’s advisable to handle such exceptions using try-catch blocks to provide a user-friendly experience and prevent the program from crashing.
  3. Can I modify the program to add more than two numbers?
    • Yes, the program can be easily modified to add more than two numbers. You would need to extend the logic to capture additional user inputs, update the variable names accordingly, and modify the output message to reflect the changes.
  4. How can I run this Java program on my computer?
    • To run the program, you need to have Java installed on your computer. Save the code in a file with a “.java” extension (e.g., AddTwoNumbers.java), compile it using a Java compiler (e.g., javac AddTwoNumbers.java), and run the compiled program (e.g., java AddTwoNumbers). Ensure your terminal or command prompt is navigated to the directory containing the Java file.
  5. Is there any way to enhance the program to handle decimal numbers?
    • Yes, the program already handles decimal numbers. The nextDouble() method used to capture user input allows for both integer and decimal input. If you want to restrict the input to integers only, you can use scanner.nextInt() instead.