Register Login

Java Simple Calculator Program

Updated Nov 24, 2020

In this tutorial, you will learn how to make a simple calculator program in java to perform basic mathematical operations like addition, subtraction, division, and multiplication according to the user input.

Approach of Program

  • First Create a Library to get input from a user
  • Create an object
  • Take first input from the user
  • Take mathematical operation from the user
  • Take second input from the user
  • Use Switch Case Statement
  • Print the output

Example of a Calculator Program in Java

//Simple java program for a calulator

//Import Scanner package
import java.util.Scanner;

// Main Class of program
public class Main
{
    //Main Function
    public static void main(String[] args) {
        //creating object for class Scanner
        Scanner input = new Scanner(System.in);
        //Declaring the variable
        double firstNumber,secondNumber;
        //Displaying a message to user what to enter
        System.out.println("Enter First Number : ");
        //Taking first number from user
        firstNumber = input.nextDouble();

        //Displaying a message to user what to enter
        System.out.println("Enter the operation : ");
        //Taking opertion from user
        char opt = input.next().charAt(0);
        //Displaying a message to user what to enter
        System.out.println("Enter Second Number : ");
        //Taking second number from user
        secondNumber = input.nextDouble();

        //Declate a variable to store the result
        double result;

        //switch case to match the entered operation
        switch(opt){
            //If entered operation is Addition
            case '+':
                result = firstNumber + secondNumber;
                break;
            //If entered operation is subtraction
            case '-':
                result = firstNumber - secondNumber;
                break;
            //If entered operation is Division
            case '/':
                result = firstNumber / secondNumber;
                break;
            //If entered operation is Multipication
            case '*':
                result = firstNumber * secondNumber;
                break;
            //If entered operation is Modulation
            case '%':
                result = firstNumber % secondNumber;
                break;
            //If entered operation is not the above all
            default:
                System.out.printf("Invalid operator");
                return;
        }
        //Print the result
        System.out.println(firstNumber+" "+opt+" "+secondNumber+" : "+result);
    }

}

Output

Enter First Number : 
5.4
Enter the operation : 
/
Enter Second Number : 
2.5
5.4 / 2.5 : 2.16

This program will take two numbers as an input and then ask the user to enter the mathematical operation (+, -, * and /). Based on the operation stored in the opt variable, which is passed to the switch statement, the operation will be performed.

If the entered operation does not match any of the cases, the message "Invalid operator" is displayed. The final output stored in the result variable is displayed in the last print statement.      


×