How to Add Two Numbers in Java?
Updated on Mar 28, 2025 | 5 min read | 7.2k views
Share:
For working professionals
For fresh graduates
More
Updated on Mar 28, 2025 | 5 min read | 7.2k views
Share:
Table of Contents
Given below is the flowchart depicting the addition of two numbers. The process is very simple. We are supposed to input two numbers and add them up. Save the answer in Sum and display the result.
Image Source: Google
Now we will take the next jump and see two programs for the addition of two numbers in Java. In the first example, we assign a value of the two numbers within the code. The second program takes the two numbers passed by the user and displays the sum.
In Java, adding two numbers requires adding two integers and printing the result. Adding two numbers in the Java programming language is straightforward; let’s look at the algorithm.
Below mentioned is the most basic algorithm for the addition of two numbers in Java :
Here are two methods to add two numbers in Java:
sum() is a function available in the integer class. Given below is a simple Java program to add two numbers using the sum() method.
Example code:
class AddTwoNumbers
{
public static void main(String args[])
{
int m = 1, m = 2, x = 3, y = 4, reqsum1, req_sum2;
reqsum1=Integer.sum(m, n);
System.out.println(“Given sum of m and n is: ” +req_sum1);
reqsum2=Integer.sum(x, y);
System.out.println(“Given sum of x and y is: ” +req_sum2);
}
}
Output:
Given sum of m and n is: 3
Given sum of x and y is: 7
The user can read input in Java and enter two numbers. Next, they will send it to the user-defined method sum().
The Java program to add two numbers using a user-defined method is implemented below.
Class AddTwoNumbers
{
public static void main(String args[])
{
int a, b, req_sum;
Scanner sc = new Scanner(System.in);
a = sc.nextInt();
b = sc.nextInt();
req_sum = final_sum(a, b);
System.out.println(“Sum of numbers ” + a + ” and ” + b + ” is: ” +reqsum );
}
public static int final_sum(int m, int n)
{
int final_sum = m + n;
return final_sum;
}
}
The sole difference between the sum of three numbers and the sum of two numbers is that there are three variables in this case.
This method of figuring out the sum of ‘N’ numbers is more inclusive. Below is the general algorithm to find the sum of ‘N’ numbers in Java.
Check out our free technology courses to get an edge over the competition.
Output:
Image Source: Google
If we observe in the above-given program, we’ll find out that the values of the numbers are specified in the program.
Check Out upGrad’s Software Development Courses
The scanner helps us to capture the input given by the user so that the values of the two numbers can be obtained from the user. Then, the program computes the sum and shows it as output.
Image Source: Google
If we see in the above program, we’ll find that at the end of the program, both the numbers are required to be furnished by the user and then the sum is calculated and displayed at the end.
The aforementioned method is not the only way to add two numbers. You can add two number using command line arguments as well. Check out the program given below to learn and understand how to add two numbers with command line arguments:
Image Source : Google
Output :
Image Source: Google
If you want to learn how to add two numbers in Java, you can use the aforementioned methods. There are other methods as well that you can use to do the same, like Using Buffered Reader, Using Method, Without Using Addition+ Operator, & Using Classes and Objects.
At upGrad, we understand the importance of practical, hands-on learning – especially when it comes to software development. As a result, our courses and training initiatives have practicality at their very core. One such initiative is Full Stack Development Bootcamp which will help you develop all the relevant skills required to excel in full-stack development.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources