In this tutorial we will learn how to add two numbers and print the result. We will try two program to add the numbers. In the first program, we just add two numbers and print the sum. The second program user is asked to enter two numbers using Scanner class and sum the numbers.
Java program to add two numbers without using Scanner
Java program to add two numbers using Scanner
The Scanner class provides the methods that allows us to read the user input. The values entered by user is read using Scanner class and stored in two variables number1 and number2. The program then calculates the sum of input numbers and displays it.
In this program we created Scanner scanner = new Scanner(System.in); This is the line which create Scanner class object and accept the user input and store into the number1 and number2 variables.Once the values are stored in variables. The addition is performed on these variables and result is displayed.
Output: