How to Add Two Integer Number in a Java Program | Addition Between Two Number ~ CodeTextPro

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


Add Two Integer Number in Java

Example: Program to Add Two Integers

public class AddTwoIntegers {

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

        int sum = first + second;

        System.out.println("The sum is: " + sum);
    }
}
When you run the program, the output will be:
Enter two numbers: 30 20
The sum is: 50      //This is the final sum result

In this program, two integer numbers3020 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 the functionprintln().

Post a Comment

0 Comments