Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    Java Program to Add Two Integers

    This Java program adds two integers. It takes input from the user in the form of an integer and performs simple arithmetic operations for addition.

    Example:

    import java.util.Scanner;
    
    class AddTwoIntegers{
    
      public static void main(String[] args) {
        
        //Variable definition and assignment
        int first, second;
        Scanner obj = new Scanner(System.in); /* create a object */
    
        //Taking user input
        System.out.print("Enter an integer: ");
        first = obj.nextInt();
        System.out.print("Enter a second integer: ");
        second = obj.nextInt();
    
        //Add the numbers
        int sum = first + second;
    
        //Print the output
        System.out.println("The sum of two integers "+first+" and "+second+" is: "+sum);
      }
    }

    Program Output:

    Enter an integer: 15
    Enter a second integer: 5
    The sum of two integers 15 and 5 is: 20


    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.