Java Program to Add Two Integers

To understand this example, you should have the knowledge of the following Java programming topics:


Example: Program to Add Two Integers

class Main {

  public static void main(String[] args) {
    
    int first = 10;
    int second = 20;

    // add two numbers
    int sum = first + second;
    System.out.println(first + " + " + second + " = "  + sum);
  }
}

Output:

10 + 20 = 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.


Also Read:

Before we wrap up, let’s put your knowledge of Java Program to Add Two Integers to the test! Can you solve the following challenge?

Challenge:

Write a function to add 10 to a given number.

  • Add 10 to number and return the result.
  • For example, if number = 5, the expected output is 15.
Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community