Java Program to Perform Addition, Subtraction, Multiplication and Division

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);
  }
}

Output of program:

Enter Two Numbers : 12
5

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

Here are few other related articles for you to read:

Courses
Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.