# Java Program to Add Two Numbers

In this post, we will learn how to add two numbers using Java Programming language.

We will be using two different ways to perform this task. They are as follows:

1. Using Standard Method
2. Using User Input

So, without further ado, let’s begin this tutorial.

## Java Program to Add Two Numbers

```// Java program to Add Two Numbers
public static void main(String[] args){

int num1 = 7, num2 = 8, sum;
sum = num1 + num2;

System.out.println("Sum of two numbers: " + sum);
}
}
```

Output

``````Sum of two numbers: 15
``````

## Java Program to Add Two Numbers Using User Input

```// Java Program to Add Two Numbers
import java.util.Scanner;
public static void main (String[] args){

// Variables
int num1, num2, sum;

Scanner sc = new Scanner(System.in);
System.out.println("Enter first number: ");
num1 = sc.nextInt();
System.out.println("Enter second number: ");
num2 = sc.nextInt();

sum = num1 + num2;

// Displaying output
System.out.println("Sum of two numbers: " + sum);
}
}
```

Output

``````Enter first number:
12
Enter second number:
13
Sum of two numbers: 25
``````

## How Does This Program Work ?

```    int num1, num2, sum;
```

In this program, we have declared three int data type variables named num1, num2 and sum.

```    // Asking for input
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number: ");
num1 = sc.nextInt();
System.out.println("Enter second number: ");
num2 = sc.nextInt();
```

Then, the user is asked to enter the first and second number.

```    // Addition
sum = num1 + num2;
```

We calculate the sum of these two numbers using the plus(+) operator.

```    // Displaying output
System.out.println("Sum of two numbers: " + sum);
```

Finally, the sum of these two numbers is displayed on the screen using the System.out.println() statement.

## Conclusion

I hope after going through this post, you understand how to add two numbers using Java Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to solve your query.