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.
Check out our free technology courses to get an edge over the competition.
Example 1: Finding the sum of two numbers
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
Example 2: Use of Scanner to compute the sum of two numbers
What is a scanner?
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
Explore our Popular Software Engineering Courses
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.
Third Example: Sum of two numbers Using command line arguments
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
In-Demand Software Development Skills
Output :
Image Source: Google
Read our Popular Articles related to Software Development
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.