Java Program to Add Two Integers

In this program, you’ll learn to store and add two integer numbers in Java. After addition, the final sum is displayed on the screen.

Example: Program to Add Two Integers

  1. public class AddTwoIntegers {
  2. public static void main(String[] args) {
  3. int first = 10;
  4. int second = 20;
  5. int sum = first + second;
  6. System.out.println("The sum is: " + sum);
  7. }
  8. }

When you run the program, the output will be:

Enter two numbers: 10 20
The sum is: 30

In this program, two integers 10 and 20 are stored in integer variables first and second respectively.

Then, first and second are added using the + operator, and its result is stored in another variable sum.

Finally, sum is printed on the screen using println() function.