X
wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, volunteer authors worked to edit and improve it over time.
This article has been viewed 204,199 times.
Learn more...
Finding the sum of two numbers is simple, but it can get tedious if the numbers are big. Here is how you can create a Java program to find the sum of two numbers.
Steps
-
1Plan your program. Finding the sum of two numbers isn't difficult, but it is always a good practice to plan your program before beginning to code. Understand that you'd need two inputs/ parameters from the user for this program: the two numbers.
-
2Write the code. Finding the sum of two numbers means the simple addition of both the numbers.[1]
- Create a separate variable to store the value of the sum. This can be of the type int.
- The formula to find the sum is:
Sum = First Number + Second Number - To get these parameters (inputs) from the user, try using the Scanner function in Java.
Advertisement -
3Display the output. Once the program has calculated the sum, display it to the user. Use the System.out.print or System.out.println (to print on a new line) function, in Java, for this.[2]
Advertisement
Community Q&A
-
QuestionCan the volume of a cuboid, cylinder, and cone be calculated by a switch statement taking suitable variables and data types?Living ConcreteTop AnswererYes. You may want to have the user specify what kind of shape before taking in any other input (such as dimensions).
-
QuestionHow do I find the sum of numbers in Java?Community AnswerEnter this code: import java.io.*; class addnumbers { public static void main(String args[]) int a=10;b=20;c; c=a+b; System.out.println("enter adding values"); }
-
QuestionHow can I find the sum of 46 and 56?Community AnswerAssume there are three integers (a, b, c): int a = 46; int b = 56; int c = a + b; System.out.println(c); Don't forget the semicolons.
Advertisement
Sample Code
mScanner.nextInt();
sum = firstNumber + secondNumber;
System.out.println("The sum of the two numbers you entered = " + sum);
}
}
References
About This Article
Advertisement