How to Assign Values to Two Numbers and Print their Sum

In this Java program, it calculates the sum of two numbers and prints the result

How to Accept Values of Two Numbers and Print their Addition

In this program

  1. We declare and initialize two integer variables, firstNumber and secondNumber, with the values 10 and 20, respectively.
  2. We calculate the sum of these two numbers and store the result in the sum variable.
  3. Finally, we use System.out.println to display the result, which will be "Addition is: 30" in this case.


public class SumOfNumbers {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int ans = a + b;
        System.out.println("Addition is : " + ans);
    }
}
Join Now


Previous Year Questions Now to Boost Your Exam Performance

LOADS OF LOGIC