The scanner allows us to capture the user input so that we can get the values of both the numbers from user. The program then calculates the sum and displays it.
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package javaexamples; import java.util.Scanner; public class JavaExamples { public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.print("Enter first number: "); int first = reader.nextInt(); System.out.print("Enter second number: "); int second = reader.nextInt(); int sum = first + second; String output = String.format("%d + %d = %d",first,second,sum); System.out.println(output); } } |
Output: