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.



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