In this post, we will see how to add two numbers in java. This is the most basic program of java programming language.
import java.util.Scanner;
public class Calculator
{
public static void main(String args[])
{
int num1, num2, sum;
// Taking input from user
Scanner scanner = new Scanner(System.in);
System.out.print("Enter two numbers : ");
num1 = scanner.nextInt();
num2 = scanner.nextInt();
// Adding two numbers
sum = num1 + num2;
System.out.print("Sum of two numbers is " +sum);
}
}
Output:
Enter two numbers : 5 7
Sum of two numbers is 12
Sum of two numbers is 12
If you notice, we have use Scanner to take input from user.