Java Programming Examples Tutorial Index

Java Number Programs

Java program to perform basic arithmetic operations of two numbers. Numbers are assumed to be integers and will be entered by the user.



This Java program asks the user to provide integer inputs to perform mathematical operations.

  • Scanner class and its functions are used to obtain inputs, and println() function is used to print on the screen.
  • Scanner class is a part of java.util package, so we required to import this package in our Java program.
  • We also required to create a object of Scanner class to call its functions.
import java.util.Scanner;
                 
public class JavaProgram
{
    public static void main(String args[])
  {
    int first, second, add, subtract, multiply;
    float devide;
    Scanner scanner = new Scanner(System.in);

    System.out.print("Enter Two Numbers : ");
    first = scanner.nextInt();
    second = scanner.nextInt();

    add = first + second;
    subtract = first - second;
    multiply = first * second;
    devide = (float) first / second;

    System.out.println("Sum = " + add);
    System.out.println("Difference = " + subtract);
    System.out.println("Multiplication = " + multiply);
    System.out.println("Division = " + devide);
  }
}

Program Output:

Enter Two Numbers : 12
5

Sum = 17
Difference = 7
Multiplication = 60
Division = 2.4


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