Java Program to add two numbers

54
views
Program to add two numbers in Java

In this program, you’ll learn how to add two numbers in java.

To properly understand this Program add two numbers, you should have the knowledge of following Java programming topics:

  • Java Variables
  • Basic Operators

Program to add two numbers

public class Add{
    public static void main(String []args){
       int num1=10;
       int num2=20;
       int res=num1+num2;
        System.out.println("The sum of "+num1+" and "+num2+" is "+res);
     }
}

Output:

The sum of 10 and 20 is 30
Java Program to add two numbers
follow @coderforevers on Instagram

In the above program first, we initialize the variables num1 and num2 with the value 10 and 20 respectively. And by using the ‘+’ operator we add the numbers and store the result in the variable res.

Above java program can also be used for performing a different mathematical operation by changing the (+) operator, we can subtract (-), multiply(*), divide(/) or find the remainder(%) of two numbers.

Check out the related programs:

Ask your questions about how to check Armstrong num in Java and clarify your/others doubts by commenting. Documentation.

Please write to us at [email protected] to report any issue with the above content or for feedback.

This Program is contributed by Ashutosh Sahu