Simple Calculator in Java using Switch case

0
39
calculator using switch Statement in Java
Advertisement

Calculators are a fundamental tool in our everyday lives, whether it’s for math homework, budgeting, or even just making simple calculations. In this blog post, we will be creating a simple calculator using the switch…case statement in Java.

To start, we will need to create a new Java project and create a new class called “Calculator”. Inside this class, we will be creating a main method where our program will run.

First, we will need to declare our variables. We will be using two double variables, “num1” and “num2”, to hold the numbers that we will be performing calculations on. We will also need a char variable, “operator”, to hold the operator that the user inputs.

Next, we will create a switch statement that will take the operator as its input. Inside the switch statement, we will use the case statement to check the operator and perform the corresponding calculation.

Here is the code for our switch statement:

switch(operator) {
    case '+':
        System.out.println(num1 + num2);
        break;
    case '-':
        System.out.println(num1 - num2);
        break;
    case '*':
        System.out.println(num1 * num2);
        break;
    case '/':
        System.out.println(num1 / num2);
        break;
    default:
        System.out.println("Invalid operator");
}

In this code, we are using the “+”, “-“, “*”, and “/” operators to perform addition, subtraction, multiplication, and division, respectively. If the operator input is none of these, the program will output “Invalid operator”.

Finally, we will need to prompt the user for their input. We can use the Scanner class to read in the user’s input for the two numbers and the operator. Here is the code for that:

Scanner input = new Scanner(System.in);
System.out.print("Enter first number: ");
num1 = input.nextDouble();
System.out.print("Enter operator: ");
operator = input.next().charAt(0);
System.out.print("Enter second number: ");
num2 = input.nextDouble();

With this code, the program will prompt the user to enter their first number, operator, and second number. The program will then store these values in the corresponding variables.

Java Program

Putting it all together, here is the full code for our simple calculator:

import java.util.Scanner;

public class Calculator {
    public static void main(String[] args) {
        double num1, num2;
        char operator;
        
        Scanner input = new Scanner(System.in);
        System.out.print("Enter first number: ");
        num1 = input.nextDouble();
        System.out.print("Enter operator: ");
        operator = input.next().charAt(0);
        System.out.print("Enter second number: ");
        num2 = input.nextDouble();
        
        switch(operator) {
            case '+':
                System.out.println(num1 + num2);
                break;
            case '-':
                System.out.println(num1 - num2);
                break;
            case '*':
                System.out.println(num1*num2);
                break;
            case '/':
                System.out.println(num1 / num2);
                break;
            default:
                System.out.println("Invalid operator");
        }
    }
}

Explanation

  1. The program imports the Scanner class from the java.util package.
  2. The program defines a class called Calculator with a main method.
  3. Within the main method, the program declares two variables, num1 and num2, to hold the two numbers entered by the user. It also declares a variable called operator to hold the arithmetic operator entered by the user.
  4. The program creates a Scanner object called input to read input from the user.
  5. The program prompts the user to enter the first number, which is stored in the num1 variable.
  6. The program prompts the user to enter the arithmetic operator, which is stored in the operator variable.
  7. The program prompts the user to enter the second number, which is stored in the num2 variable.
  8. The program uses a switch statement to evaluate the operator variable and perform the corresponding arithmetic operation using the num1 and num2 variables.
    • If the operator entered by the user is “+”, the program performs num1 + num2 and prints the result.
    • If the operator entered by the user is “-“, the program performs num1 – num2 and prints the result.
    • If the operator entered by the user is “*”, the program performs num1 * num2 and prints the result.
    • If the operator entered by the user is “/”, the program performs num1 / num2 and prints the result.
  9. If the operator entered by the user is not one of the accepted operators (+, -, *, /), the program prints “Invalid operator”.

Latest Posts:

LEAVE A REPLY

Please enter your comment!
Please enter your name here