Java Program add Two Numbers

add 2 numbers java program
Reading Time: 7 minutes

Introduction:

The Java program you provided is a simple console-based application that adds two numbers. It utilizes the Scanner class to take user input for two numbers, performs the addition operation, and then displays the result.

Explanation:

  1. Import Statements:

The program starts by importing the Scanner class from the java.util package. This class is used to take user input from the console.

  1. Class Declaration:

The program defines a class named AddTwoNumbers. Java programs typically consist of one or more classes, and the main method within the class serves as the entry point for execution.

  1. Main Method:

The main method is the starting point of the program. It is where the execution begins.

  1. Scanner Initialization:

An instance of the Scanner class is created to read input from the user.

  1. User Input:

The program prompts the user to enter the first number, reads the input as a double, and stores it in the variable num1. The same process is repeated for the second number.

  1. Addition Operation:

The two numbers entered by the user are added, and the result is stored in the variable sum.

  1. Output:

The program prints the result, indicating the sum of the two numbers.

  1. Scanner Closing:

The Scanner is closed to release system resources and avoid potential resource leaks.

Algorithm:

  1. Import the Scanner class.
  2. Define the class AddTwoNumbers.
  3. Declare the main
  4. Create a Scanner instance for user input.
  5. Prompt the user to enter the first number and read it.
  6. Prompt the user to enter the second number and read it.
  7. Add the two numbers and store the result in the sum
  8. Print the result using a formatted message.
  9. Close the Scanner to release resources.

Code :

				
					import java.util.Scanner;
public class AddTwoNumbers {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the first number: ");
        double num1 = scanner.nextDouble();
        System.out.print("Enter the second number: ");
        double num2 = scanner.nextDouble();
        double sum = num1 + num2;
        System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum);
        // Close the scanner to avoid resource leak
        scanner.close();
    }
}

				
			

Input :

				
					Enter the first number: 65
Enter the second number: 68
				
			

Output :

				
					The sum of 65.0 and 68.0 is: 133.0
				
			

This program takes two numbers as input from the user, adds them, and then prints the result.

FAQs:

  1. Question: How can I create a Java program to add two numbers?

Answer: To create a Java program for adding two numbers, you can use the Scanner class to take user input, perform the addition, and display the result.

  1. Question: What is the purpose of a Java program for the addition of two numbers?

Answer: The purpose is to demonstrate a simple Java program that takes two numbers as input, adds them together, and outputs the result.

  1. Question: Can you provide an example of a Java program that adds two numbers?

Answer: Certainly! A Java program for adding two numbers typically involves using the Scanner class to read user input and performing the addition operation.

  1. Question: How do I write a Java program of adding two numbers?

 Answer: Writing a Java program of adding two numbers involves creating a class, using the Scanner class for input, performing the addition, and printing the result.

  1. Question: What is the syntax for a Java program to add two numbers?

Answer: The syntax involves creating a class, using the Scanner class to obtain user input, adding the numbers, and then printing the result.

  1. Question: What does a Java program sum of two numbers look like?

Answer: A Java program for the sum of two numbers typically includes user prompts, input handling with Scanner, addition logic, and output display.

  1. Question: How can I implement a Java program for two number addition?

Answer: You can implement a Java program for two number addition by using the Scanner class for input, performing the addition, and printing the result.

  1. Question: Can you share a simple Java program to add two numbers?

Answer: Certainly! A simple Java program to add two numbers involves user input, addition logic, and displaying the sum. You can easily adapt and customize it for your needs.