# Java Program to Make a Simple Calculator using Switch Case

• Write a Java program to make a simple calculator using switch case statement which perform addition, subtraction, multiplication and division or two numbers.

Given two integers and an arithmetic operator, we have to perform the specific arithmetic operation on given integer operands using a switch case statement and print the result on screen.

## Java program for simple calculator using switch statement

In this java program, we first takes two integer operands and an arithmetic operator as input from user. The operator is stored in a character variable 'op'. This calculator only support addition, subtraction, multiplication and division(+, - , * and /) operators and for any other operator it prints error message on screen saying "Unsupported Operation". It uses switch case statement to select an arithmetic operation based on the 'op' variable.

```package com.tcc.java.programs;

import java.util.Scanner;

/**
* Java Program to Make a Simple Calculator using Switch Statement
*/
public class Calculator {
public static void main(String[] args) throws myException {
int a, b;
char op;

Scanner scanner;
scanner = new Scanner(System.in);
// Take two numbers as input from user
System.out.println("Enter Two Integers");
a = scanner.nextInt();
b = scanner.nextInt();

// Taking operator as input from user
System.out.println("Enter an Operator");
op = scanner.next().charAt(0);

switch (op) {
case '+':
System.out.format("%d + %d = %d\n", a, b, a + b);
break;
case '-':
System.out.format("%d - %d = %d\n", a, b, a - b);
break;
case '*':
System.out.format("%d * %d = %d\n", a, b, a * b);
break;
case '/':
System.out.format("%d / %d = %d\n", a, b, a / b);
break;
default:
System.out.println("ERROR: Unsupported Operation");
}
}
}
```
Output
```Enter Two Integers
10 4
Enter an Operator
+
10 + 4 = 14
```
```Enter Two Integers
4 7
Enter an Operator
*
4 * 7 = 28
```
```Enter Two Integers
2 3
Enter an Operator
^
ERROR: Unsupported Operation
```

Recommended Posts
 Java Program to Calculate Simple Interest Java Program to Calculate Compound Interest Java Program to Check If a Year is Leap Year or Not Java Program to Convert Celsius to Fahrenheit Java Program to Convert Fahrenheit to Celsius Java Program to Convert Decimal to Binary Numbers Java Program to calculate area and circumference of circle Java Program to Find Area of Right Angled Triangle Java Program to Add Subtract Multiply and Divide Two Numbers Java Program to Find LCM and GCD of Two Numbers Java Program to Calculate Average and Percentage Marks All Java Programs
 Previous Next