How to Find the Sum of Two Numbers in Java
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.Ad
-
2Write the code. Finding the sum of two numbers means the simple addition of both the numbers.
- 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.
-
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.Ad
We could really use your help!
robots?
rate articles?

business?

ice skating?

music production?

Sample Code
import java.util.Scanner; public class main_class { public static void main(String[] args){ int sum = 0, firstNumber, secondNumber; Scanner inputNumScanner = new Scanner(System.in); System.out.println("Enter First Number: "); firstNumber = inputNumScanner.nextInt(); System.out.println("Enter Second Number: "); secondNumber = inputNumScanner.nextInt(); sum = firstNumber + secondNumber; System.out.println("The sum of the two numbers you entered = " + sum); } }
Tips
- Try expanding your program to perform multiple mathematical calculations.
- Try making a GUI, which will make the program much more interactive and easier to use.
Article Info
Categories: Java
In other languages:
Español: encontrar la suma de dos números en Java, Italiano: Calcolare la Somma di Due Numeri in Java, Русский: найти сумму двух чисел в Java, Português: Encontrar a Soma de Dois Números no Java
Thanks to all authors for creating a page that has been read 59,625 times.
About this wikiHow