Java Two-Number Addition program
- Get link
- X
- Other Apps
Java Two-Number Addition.

Two number addition program in java
javaimport java.util.Scanner;
public class AdditionProgram {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = input.nextInt();
System.out.print("Enter the second number: ");
int num2 = input.nextInt();
int sum = num1 + num2;
System.out.println("The sum of " + num1 + " and " + num2 + " is " + sum);
}
}
Explanation:
- We first import the Scanner class from the java.util package which allows us to read input from the user.
- Inside the main method, we create an instance of the Scanner class called "input".
- We then prompt the user to enter the first number by displaying the message "Enter the first number: " and read the input using input.nextInt(), which reads an integer value.
- Similarly, we prompt the user to enter the second number and read the input using input.nextInt().
- We calculate the sum of the two numbers and store it in the variable "sum".
- Finally, we display the sum using System.out.println(). The output message uses string concatenation to display the original input values and the calculated sum.
- Get link
- X
- Other Apps
Comments
Post a Comment