Java Program to add two integer numbers


February 13, 2022, Learn eTutorial
1693

How to declare a class in java?

Here we are explaining how to write a java program to find the sum of two integer numbers So first we have to declare the class AdditionOfTwoNumbers with a public modifier. The name of the class should start with a capital letter. And we can set the modifier as public, private, or protected. An Example of a class declaration is shown below.



public class TestClass

{

}
 

Here the name of the class is TestClass and the modifier is public.

How to implement a java Program to calculate the sum of two integer numbers?

First, we declare the class AdditionOfTwoNumbers then we open the main() function. Then we declare number1,number2, result as datatype integer. Then we set number1=10 and number2=20. Then we perform the addition by adding number1 and number2 into the variable result. Now the value of the result becomes 30. Then we can display the sum of two numbers as the value of the result using system.out.println.

ALGORITHM

STEP 1: Declare the class AdditionOfTwoNumbers with a public modifier 

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Declare the variables number1,number2,result as integer.

STEP 4: Assign number1=10,number=20.

STEP 5: Calculate the result as number1+number2 

STEP 6: Display the result using system.out.println.

Java Source Code

                                          public class AdditionOfTwoNumbers {

   public static void main(String[] args) {
        
      int number1=10,number2=20,result;//Declare the variables 
      result=number1+number2;//Calculate the sum 

      System.out.println("Sum of two integer numbers: "+result);//Display the result
   }
}
                                      

OUTPUT

Sum of two integer numbers: 30