Java Programming Examples Tutorial Index
Java Number Programs

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.


import java.util.Scanner;

class AddTwoIntegers{

  public static void main(String[] args) {
    //Variable definition and assignment
    int first, second;
    Scanner obj = new Scanner(; /* 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