Java Program for Addition, Subtraction, Multiplication and Division

In this example, you'll get to learn, how to perform addition, subtraction, multiplication, and division of any two numbers in Java. Before starting the actual program, let's first create a very simple program that performs four basic mathematical operations without user input.

Addition, Subtraction, Multiplication, Division without User Input

The question is, write a Java program that performs four basic mathematical operations such as addition, subtraction, multiplication, and division. Here is its answer. I've not allowed user to feed the input for this program, but later on, this program is modified to allow user to provide the input at run-time of the program to perform operation of two desired numbers.

public class fresherearth
{
   public static void main(String[] args)
   {
      int num1 = 20, num2 = 15, res;
      
      res = num1 + num2;
      System.out.println("Addition Result = " + res);
      res = num1 - num2;
      System.out.println("Subtraction Result = " + res);
      res = num1 * num2;
      System.out.println("Multiplication Result = " + res);
      res = num1 / num2;
      System.out.println("Division Result = " + res);
   }
}

The output produced by this Java program will exactly be:

java addition subtraction multiplication division

Addition, Subtraction, Multiplication, and Division with User Input

This program is basically the modified version of previous program, as it allows user to enter numbers for the program. Rest of the things are similar to previous program.

import java.util.Scanner;

public class fresherearth
{
   public static void main(String[] args)
   {
      Scanner scan = new Scanner(System.in);
      int num1, num2, res;
      
      System.out.println("Enter the First Number: ");
      num1 = scan.nextInt();
      System.out.println("Enter the Second Number: ");
      num2 = scan.nextInt();
      
      res = num1 + num2;
      System.out.println("\nAddition Result = " + res);
      res = num1 - num2;
      System.out.println("Subtraction Result = " + res);
      res = num1 * num2;
      System.out.println("Multiplication Result = " + res);
      res = num1 / num2;
      System.out.println("Division Result = " + res);
   }
}

Here is its sample run with user input 44 as first and 23 as second number:

addition subtraction multiplication division java

Basic Mathematical Operation in Java - Complete Version

This is the complete version of the above program, as it handles errors too. Because, sometime user enters wrong or invalid inputs like c or # in place of number. Therefore, the program must be created in a way to handle those type of errors.

import java.util.Scanner;
import java.util.InputMismatchException;

public class fresherearth
{
   public static void main(String[] args)
   {
      Scanner scan = new Scanner(System.in);
      double num1, num2, res;
      
      System.out.println("Enter the First Number: ");
      try
      {
         num1 = scan.nextDouble();
      }
      catch(InputMismatchException e)
      {
         System.out.println("\nInvalid Input!");
         System.out.println("Exception Name: " + e);
         return;
      }
		
      System.out.println("Enter the Second Number: ");
      try
      {
         num2 = scan.nextDouble();
      }
      catch(InputMismatchException e)
      {
         System.out.println("\nInvalid Input!");
         System.out.println("Exception Name: " + e);
         return;
      }
      
      res = num1 + num2;
      System.out.println("\nAddition Result = " + res);
      res = num1 - num2;
      System.out.println("Subtraction Result = " + res);
      res = num1 * num2;
      System.out.println("Multiplication Result = " + res);
      res = num1 / num2;
      System.out.println("Division Result = " + res);
   }
}

Here is its sample run with user input 32 as first and 31 as second number:

java add subtract multiply divide

Here is another sample run with an invalid user input as second number say 32 as first, but c as second number:

java add subtract multiply divide with user input

If you enter any invalid input as first number, then the program prints error message say Invalid Input! along with exception name. Also the second number will not get received from user, by the program, because I've used return keyword to stop the remaining execution of the program, when user feed an invalid input. For example, let's take another sample run with user input fresherearth as first number:

add subtract multiply divide java

Add, Subtract, Multiply, Divide based on User's Choice

This is the minimal version of the above program, as it performs only single desired operation at a time, based on user's choice.

import java.util.Scanner;

public class fresherearth
{
   public static void main(String[] args)
   {
      Scanner scan = new Scanner(System.in);
      float num1, num2, res;
      char ch;
      
      System.out.println("Enter any Two Numbers: ");
      num1 = scan.nextFloat();
      num2 = scan.nextFloat();
      System.out.println("Enter the Operator (+, -, *, /): ");
      ch = scan.next().charAt(0);
      if(ch == '+') res = num1 + num2;
      else if(ch == '-') res = num1 - num2;
      else if(ch == '*') res = num1 * num2;
      else if(ch == '/') res = num1 / num2;
      else
      {
         System.out.println("\nInvalid Input");
         return;
      }
      System.out.println("\nResult = " + res);
   }
}

Here is its sample run with user input 10 and 30 as first and second number, and + as operator to perform the addition of these two numbers only:

java perform mathematical operation based on user

The above program uses if...else if...else conditional statements to check and perform the operation based on the operator entered by user.

Same Program in Other Languages

Java Online Test


« Previous Program Next Program »