Last Updated: 13 August, 2022

On this tutorial page we are going to learn how to make a simple calculator in Java using switch cases that performs addition (+), subtraction (-), multiplication (*), and division (/) operations based on the user's input.

This program will ask the user to enter the operator (+, -, * and /) and two numbers. Based on the entered operator, it will generate the result.

Let's see the Java program implementation.

If we run the program with all possible inputs, we will get the following output:

Output 1 for add operation (+)

Sample Calculator for Arithmetic Operation Valid Operators: + (Add), - (Sub), * (Multiply), or / (Div) Please enter a valid operator: + Enter first number: 10.50 Enter second number: 20.55 Addition output: 31.05

Output 2 for subtract operation (-)

Sample Calculator for Arithmetic Operation Valid Operators: + (Add), - (Sub), * (Multiply), or / (Div) Please enter a valid operator: - Enter first number: 200 Enter second number: 50.55 Subtraction output: 149.45

Output 3 for multiplication operation (*)

Sample Calculator for Arithmetic Operation Valid Operators: + (Add), - (Sub), * (Multiply), or / (Div) Please enter a valid operator: * Enter first number: 100.1 Enter second number: 5 Multiplication output: 500.5

Output 4 for division operation (/)

Sample Calculator for Arithmetic Operation Valid Operators: + (Add), - (Sub), * (Multiply), or / (Div) Please enter a valid operator: / Enter first number: 400 Enter second number: 5 Division output: 80.0

Output 5 for invalid input

Sample Calculator for Arithmetic Operation Valid Operators: + (Add), - (Sub), * (Multiply), or / (Div) Please enter a valid operator: # Enter first number: 10 Enter second number: 20 Please enter a valid operator

Here, with all five possible inputs, we ran this program and showed the output above.

That's all guys, hope this Java Program is helpful for you.

Happy Learning... 😀

If you have any query or suggestion regarding this artical please share with us

`feedback@javabytechie.com`