Addition program that inputs two numbers then displays their sum.Our next application reads (or inputs) two integers (whole numbers, such as –22, 7, 0 and 1024) typed by a user at the keyboard, computes their sum and displays it.
Java Program to Add Two Integers
import java.util.Scanner;
public class Addition{
public static void main(String[] args){
// create a scanner to obtain input from the command window
Scanner input = new Scanner(System.in);
int number1;
int number2;
int sum;
System.out.print("Enter first integer: ");
number1 = input.nextInt();
System.out.print("Enter second integer: ");
number2 = input.nextInt();
sum = number1 + number2;
System.out.printf("Sum is %d\n", sum);
}
}
Output of Java Program to Add Two Integers
Enter first integer: 40
Enter second integer: 70
Sum is 110