Java program to create calculator using switch case :


According to the title, you know what we have to learn with this article.

Basically you think that we will learn how to create a calculator using Java programming but also we will learn about switch cases.

In this article, we will learn about how to create a simple calculator.

which performs addition, subtraction, multiplication, and division of two numbers.

We will take input from the user in this program. The program will take three user inputs. Which are two numbers and one character? The character may be (+ – * / ), which is basically operation that you have to do between two numbers.

Example – Program to create Calculator using Switch case with Java

package ct;
import java.util.Scanner;
public class switchCase {
public static void main(String[] args) {

    Scanner sc = new Scanner(;

    System.out.println("enter the first number");
    float a = sc.nextFloat();

    System.out.println("enter second number");
    float b = sc.nextFloat();

    System.out.println("enter operator [ + , - , * , / ]");
    char operation = sc.nextLine().charAt(0);

    float result = 0;

    switch(operation) {

    case '+' :
        result = a + b;
    case '-' :
        result = a - b ;
    case '*' :
        result = a * b ;
    case '/' :
        result = a / b ;    
    default :
          System.out.println("invalid operation");
      System.out.println("result is " + result);  

You can go with the above program to create a calculator program using a switch case with Java.

Output –

enter the first number
enter second number
enter operator [ + , – , * , / ]
result is 15.0

Explanation of the code –

For taking user input first you have to import java.util.Scanner.

If you want to learn how to take user input click here – How to take user input in Java

So, first of all, we take the first number from the user and then the second number.

For performing the operation between two number we take one character which is maybe ( + – * / ).

Now you have to use a switch case to create a calculator you can also create a calculator using if and else statement.

But when you use if and else to create a calculator the program becomes more complicated.

In switch case, you just have to write the cases which you want to do between two numbers or for any other program you will create.

System.out.println("enter operator [ + , - , * , / ]");
char operation = sc.nextLine().charAt(0);

In this code, you definitely have some difficulty so let me describe this code.

You have to take one operation from the user. But you can not use the char Data type for performing this task. So we use the string and take !st character of the string which is maybe ( + – * / ).

If you want to learn more about the Switch case read the article which is given below.

Switch Case statement in Java with example.

I hope you all like this article do share with your friends. And if you have any query comment below in the comment section.

If you want to learn switch case you can go with the youtube video which is given below –

Switch case


A digital marketer, programmer, and WordPress developer who is always looking for new technology stuff and news. Saurabh has 1+ years of experience in writing content and in web development.


How To Recover Photos From Google Photos - computer technologer · May 3, 2020 at 9:54 am

[…] How to create a Calculator using Java […]

10 Best Gmail Alternative for Email in 2020 - computer technologer · May 5, 2020 at 2:01 pm

[…] How to create a Calculator using Java […]

Leave a Reply

Your email address will not be published. Required fields are marked *