Java Program That Takes Two Numbers As Input And Prints Their Sum
Chapter:
Miscellaneous
Last Updated:
27-05-2023 12:59:54 UTC
Program:
/* ............... START ............... */
import java.util.Scanner;
public class SumCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int number1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int number2 = scanner.nextInt();
int sum = number1 + number2;
System.out.println("The sum of the two numbers is: " + sum);
}
}
/* ............... END ............... */
Output
Enter the first number: 10
Enter the second number: 5
The sum of the two numbers is: 15
Notes:
-
In this program, we use the Scanner class to read input from the user. We prompt the user to enter the first number, read it using scanner.nextInt(), and store it in the variable number1. Similarly, we prompt for the second number and store it in number2.
- Then, we calculate the sum of the two numbers by adding number1 and number2, and store the result in the variable sum. Finally, we print the sum to the console using System.out.println().
- When you run this program, it will ask you to enter the two numbers, and once you provide the input, it will display the sum of the two numbers.