Java Program to add two numbers

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

If you notice, we have use Scanner to take input from user.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.