Java program to add two numbers

Java program to add two numbers is one of the common java interview question.

Java program to add two numbers

In this java program we are taking input from user to add two numbers using scanner.

Also read – add two numbers using method in java

Let’s see java program,

import java.util.Scanner;

public class AddTwoNumbers
{
   public static void main(String[] args)
   {
      int a, b, c;
      System.out.println("Please enter two integers to add : ");
      Scanner sc = new Scanner(System.in);

      a = sc.nextInt();
      b = sc.nextInt();
      c = a + b;

      System.out.println("Sum of two numbers = " + c);
      sc.close();
   }
}

Output:

java program to add two numbers


Now let’s see without using scanner class,

public class AddTwoNumbersDemo
{
   public static void main(String[] args)
   {
      int a = 5, b = 5, c;
      c = a + b;
      System.out.println("Sum of two integers : " + c);
   }
}

Output:

10