Add Two Integers in Java

EXCERCISE:

Write a Java Program that adds two integers.

Click Here to View the Solution!
public class AddingTwoIntegers {

       public static void main(String[] args) {
         //variables of data type integers are initiated to store the numbers
         int firstInt = 30;
         int secondInt = 15;
  
     System.out.println("Numbers to be added are: " + firstInt + " " + secondInt);
          // new integer sum is initiated 
          // the value is kept equal to the sum of both numbers
          int sum = firstInt + secondInt;
          
         System.out.println("The sum is: " + sum);
     }
}

Click Here to View the Output!
Numbers to be added are: 30 15
The sum is: 45
Click Here to View the Explanation!
  • In this program, two integer variables firstInt and secondInt are initiated.
  • Then stored 30 and 15  in them respectively.
  • Another variable sum is declared to store the sum of  the variables  firstInt and secondInt using the + operator.
  • Finally println() function prints  sum on console screen.

%d bloggers like this: