Simple Calculator in Java

In this tutorial, we'll implement a very simple calculator in Java supporting addition, subtraction, multiplication, and division operations.

We'll also take the operator and operands as inputs and process the calculations based on them.

This tutorial is useful for beginners to understand how to build a very simple calculator using Core Java.

We will use the Scanner class to read user inputs such as operator, number 1, and number 2.

We will make a simple calculator using if-else as well as switch-case statement:

  1. Simple calculator using if-else in Java
  2. Simple calculator using switch case in Java

Simple calculator using if-else in Java

package com.java.calculator;

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

public class BasicCalculatorIfElse {

	public static void main(String[] args) {

		System.out.println("---------------------------------- \n" + "Welcome to Basic Calculator \n"
				+ "----------------------------------");
		System.out.println("Following operations are supported : \n" + "1. Addition (+) \n" + "2. Subtraction (-) \n"
				+ "3. Multiplication (* OR x) \n" + "4. Division (/) \n");

		try(Scanner scanner = new Scanner(System.in))
		{
			System.out.println("Enter an operator: (+ OR - OR * OR /) ");
			char operation = scanner.next().charAt(0);

			if (!(operation == '+' || operation == '-' || operation == '*' || operation == 'x' || operation == '/')) {
				System.err.println("Invalid Operator. Please use only + or - or * or /");
			}

			System.out.println("Enter First Number: ");
			double num1 = scanner.nextDouble();

			System.out.println("Enter Second Number: ");
			double num2 = scanner.nextDouble();

			if (operation == '/' && num2 == 0.0) {
				System.err.println("Second Number cannot be zero for Division operation.");
			}

			if (operation == '+') {
				System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
			} else if (operation == '-') {
				System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
			} else if (operation == '*' || operation == 'x') {
				System.out.println(num1 + " x " + num2 + " = " + (num1 * num2));
			} else if (operation == '/') {
				System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
			} else {
				System.err.println("Invalid Operator Specified.");
			}
		} catch (InputMismatchException exc) {
			System.err.println(exc.getMessage());
		}
	}
}

Output :

---------------------------------- 
Welcome to Basic Calculator 
----------------------------------
Following operations are supported : 
1. Addition (+) 
2. Subtraction (-) 
3. Multiplication (* OR x) 
4. Division (/) 

Enter an operator: (+ OR - OR * OR /) 
+
Enter First Number: 
10
Enter Second Number: 
20
10.0 + 20.0 = 30.0

Simple calculator using switch case in Java

package com.java.calculator;

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

public class BasicCalculator {
	public static void main(String[] args) {

		System.out.println("---------------------------------- \n" + "Welcome to Basic Calculator \n"
				+ "----------------------------------");
		System.out.println("Following operations are supported : \n" + "1. Addition (+) \n" + "2. Subtraction (-) \n"
				+ "3. Multiplication (* OR x) \n" + "4. Division (/) \n");

		try (Scanner scanner = new Scanner(System.in)) {
			System.out.println("Enter an operator (+, -, *, /):");
			char operation = scanner.next().charAt(0);

			if (!(operation == '+' || operation == '-' || operation == '*' || operation == 'x' || operation == '/')) {
				System.err.println("Invalid Operator. Please use only + or - or * or /");
			}

			System.out.println("Enter First Number: ");
			double num1 = scanner.nextDouble();

			System.out.println("Enter Second Number: ");
			double num2 = scanner.nextDouble();

			if (operation == '/' && num2 == 0.0) {
				System.err.println("Second Number cannot be zero for Division operation.");
			}

			switch (operation) {
			case '+':
				System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
				break;
			case '-':
				System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
				break;
			case '*':
				System.out.println(num1 + " x " + num2 + " = " + (num1 * num2));
				break;
			case 'x':
				System.out.println(num1 + " x " + num2 + " = " + (num1 * num2));
				break;
			case '/':
				System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
				break;
			default:
				System.err.println("Invalid Operator Specified.");
				break;
			}
		} catch (InputMismatchException exc) {
			System.err.println(exc.getMessage());
		}
	}
}

Output :

---------------------------------- 
Welcome to Basic Calculator 
----------------------------------
Following operations are supported : 
1. Addition (+) 
2. Subtraction (-) 
3. Multiplication (* OR x) 
4. Division (/) 

Enter an operator (+, -, *, /):
-
Enter First Number: 
20
Enter Second Number: 
10
20.0 - 10.0 = 10.0

Comments