Java Program to Create a Simple Calculator

Write a Java Program to create a Simple Calculator using a switch case and Else if statement. The first example allows entering two numeric values and the operator to perform calculations.

Next, we used the switch case to perform computations based on the given operator. For example, if we enter +, the switch case will perform addition. Here, we also use the default case for the wrong operator to notify the suer about the wrong entry.

package SimpleNumberPrograms;
import java.util.Scanner;
public class Calculator {
	private static Scanner sc;
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		double a, b, result;	
		System.out.print("Please Enter any Two Numbers = ");
		a = sc.nextDouble();
		b = sc.nextDouble();	
		System.out.print("Enter any Operator from +, - , /, *, % = ");
		char operator = sc.next().charAt(0);	
		switch(operator) {
		case '+':
			result = a + b;
			break;
		case '-':
			result = a - b;
			break;
		case '*':
			result = a * b;
			break;
		case '/':
			result = a / b;
			break;
		case '%':
			result = a % b;
			break;
		default:
			System.out.println("You have entered incorrect operator");
			return;
		}		
		System.out.printf("%.2f %c %.2f = %.2f", a, operator, b, result);
	}
}
Java Program to create a Simple Calculator

Let me enter the wrong symbol.

Please Enter any Two Numbers = 11 4
Enter any Operator from +, - , /, *, % = #
You have entered incorrect operator

Java Program to create a Simple Calculator using else if

In this example, we replaced the switch with else if conditions to create a simple calculator.

package SimpleNumberPrograms;
import java.util.Scanner;
public class Example2 {

	private static Scanner sc;
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		double a, b, result = 0;
		
		System.out.print("Please Enter any Two Numbers = ");
		a = sc.nextDouble();
		b = sc.nextDouble();
		
		System.out.print("Enter any Operator from +, - , /, *, % = ");
		char operator = sc.next().charAt(0);
		
		if(operator == '+') {
			result = a + b;
		}
		else if(operator == '-') {
			result = a - b;
		}
		else if(operator == '*') {
			result = a * b;
		}
		else if(operator == '/') {
			result = a / b;
		}
		else if(operator == '%') {
			result = a % b;
		}
		else {
			System.out.println("You have entered incorrect operator");
		}
		
		System.out.printf("%.2f %c %.2f = %.2f", a, operator, b, result);
	}
}
Please Enter any Two Numbers = 20 32
Enter any Operator from +, - , /, *, % = -
20.00 - 32.00 = -12.00

Using functions

This program is the same as the above example. However, we created a separate function for each calculation. In real-time, it would be wise to create a separate class with these methods and create an instance of the same to call them in the main program.

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] xrgs) {
		sc = new Scanner(System.in);
		double x, y, result = 0;

		System.out.print("Enter Two Numbers = ");
		x = sc.nextDouble();
		y = sc.nextDouble();

		System.out.print("Enter anyy opt from +, - , /, *, % = ");
		char opt = sc.next().charAt(0);

		if (opt == '+') {
			result = add(x, y);
		} else if (opt == '-') {
			result = sub(x, y);
		} else if (opt == '*') {
			result = mul(x, y);
		} else if (opt == '/') {
			result = div(x, y);
		} else if (opt == '%') {
			result = mod(x, y);
		} else {
			System.out.println("You have entered incorrect option");
		}

		System.out.printf("%.2f %c %.2f = %.2f", x, opt, y, result);
	}

	static double add(double x, double y) {
		return x + y;
	}

	static double sub(double x, double y) {
		return x - y;
	}

	static double mul(double x, double y) {
		return x * y;
	}

	static double div(double x, double y) {
		return x / y;
	}

	static double mod(double x, double y) {
		return x % y;
	}

}
Enter Two Numbers = 22
8
Enter anyy opt from +, - , /, *, % = %
22.00 % 8.00 = 6.00