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

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:
- Program to Convert String to Date
- Program to Get Current Date/time
- Program to Convert Milliseconds to Minutes and Seconds
- Program to Calculate Standard Deviation
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