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

  1. 1
    Plan 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.
  2. 2
    Write 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
  3. 3
    Display 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]
  4. Advertisement

Community Q&A

  • Question
    Can the volume of a cuboid, cylinder, and cone be calculated by a switch statement taking suitable variables and data types?
    Living Concrete
    Living Concrete
    Top Answerer
    Yes. You may want to have the user specify what kind of shape before taking in any other input (such as dimensions).
  • Question
    How do I find the sum of numbers in Java?
    Community Answer
    Community Answer
    Enter 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"); }
  • Question
    How can I find the sum of 46 and 56?
    Community Answer
    Community Answer
    Assume 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);
    }
}

About This Article

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.
How helpful is this?
Co-authors: 7
Updated: September 20, 2021
Views: 204,199
Categories: Java
Advertisement