๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

WAP to create a calculator using a switch case in Java  


Logical Steps:
  • To create simple calculator using switch case, follow these steps:
    1. Take User Input:
      • Ask the user to enter two numbers.
      • Ask the user to choose an arithmetic operation (+, -, *, /, or %).
    2. Use a Switch Case for the Operation:
      • Use the switch statement to handle the selected operation.
      • Perform the appropriate arithmetic operation based on the user's choice.
    3. Perform the Calculation:
      • Case-wise execution will calculate the result.
      • Handle the default case for invalid operations.
    4. Display the Result:
      • Print the result of the selected arithmetic operation.
Programs:
  • Below is the simple program:
    public class CalculatorSwitchCase
    {
        public static void main(String[] args)
        {
            double no1=10, no2=30;
    
            char operator = '*';
    
            double result;
    
            // Use switch case to perform the selected operation
            switch (operator)
            {
                case '+':
                    result = no1 + no2;
                    System.out.println("Result: " + result);
                    break;
                case '-':
                    result = no1 - no2;
                    System.out.println("Result: " + result);
                    break;
                case '*':
                    result = no1 * no2;
                    System.out.println("Result: " + result);
                    break;
                case '/':
                    if (no2 != 0) {
                        result = no1 / no2;
                        System.out.println("Result: " + result);
                    }
                    else
                    {
                        System.out.println("Error: Division by zero is not allowed.");
                    }
                    break;
                case '%':
                    if (no2 != 0)
                    {
                        result = no1 % no2;
                        System.out.println("Result: " + result);
                    }
                    else
                    {
                        System.out.println("Error: Modulus by zero is not allowed.");
                    }
                    break;
                default:
                    System.out.println("Invalid operator. Please choose a valid operation.");
            }
        }
    }
    Output:
    Result: 300.0
  • Below is the program by taking user input:
    import java.util.Scanner;
    
    public class CalculatorSwitchCase
    {
        public static void main(String[] args)
        {
            Scanner scanner = new Scanner(System.in);
    
            // Take two numbers as input
            System.out.print("Enter the first number: ");
            double no1 = scanner.nextDouble();
    
            System.out.print("Enter the second number: ");
            double no2 = scanner.nextDouble();
    
            // Ask the user to choose an operation
            System.out.println("Choose an operation (+, -, *, /, %): ");
            char operator = scanner.next().charAt(0);
    
            double result;
    
            // Use switch case to perform the selected operation
            switch (operator)
            {
                case '+':
                    result = no1 + no2;
                    System.out.println("Result: " + result);
                    break;
                case '-':
                    result = no1 - no2;
                    System.out.println("Result: " + result);
                    break;
                case '*':
                    result = no1 * no2;
                    System.out.println("Result: " + result);
                    break;
                case '/':
                    if (no2 != 0)
                    {
                        result = no1 / no2;
                        System.out.println("Result: " + result);
                    }
                    else
                    {
                        System.out.println("Error: Division by zero is not allowed.");
                    }
                    break;
                case '%':
                    if (no2 != 0)
                    {
                        result = no1 % no2;
                        System.out.println("Result: " + result);
                    }
                    else
                    {
                        System.out.println("Error: Modulus by zero is not allowed.");
                    }
                    break;
                default:
                    System.out.println("Invalid operator. Please choose a valid operation.");
            }
    
            // Close the scanner
            scanner.close();
        }
    }
    Output:
    Enter the first number: 10
    Enter the second number: 20
    Choose an operation (+, -, *, /, %): 
    +
    Result: 30.0
Program Explanation:
  • Input and Variables:
    • The program takes two numbers as input (no1 and no2).
    • The user selects an arithmetic operation (+, -, *, /, %).
  • Switch Case for Operations:
    • The switch statement checks the operator entered by the user and performs the corresponding operation.
    • Addition (+): Adds no1 and no2.
    • Subtraction (-): Subtracts no1 from no2.
    • Multiplication (*): Multiplies no1 and no2.
    • Division (/): Divides no1 by no2. If no2 is zero, it displays an error message.
    • Modulus (%): Computes the remainder when no1 is divided by no2.
  • Default Case:
    • If the user enters an invalid operator, the default case displays an error message.
  • Display the Result:
    • The program prints the result of the selected arithmetic operation.
  • Close Scanner:
    • The scanner.close() method is used to close the input stream and release resources.