Challenge: Making a Calculator

In this exercise, you are required to implement a calculator by using Switch statements.

Problem Statement

Write a code which will take:

  • Two variables named num1 and num2

  • a string type variable called Operator which will be passed as input to our switch statement.

  • The Operator variable can be passed the following:

    • +,-,* and /
  • Use switch statements to compute:

    • addition,
    • subtraction,
    • multiplication
    • division

Note: For the default case, simply print the statement echo "Wrong operator";.

Sample Input

num1 = 5.5
num2 = 6.5
Operator = '+'

Sample Output

Output = 12

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy