Search⌘ K

Challenge 2: Making a Calculator

Learn how to implement a Perl calculator that takes two numbers and an operator as inputs. Explore using conditional statements like given and when to perform basic arithmetic operations including addition, subtraction, multiplication, and division. This lesson helps build practical skills with Perl operators and control flow.

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 given statement

  • The $Operator variable can have any of the following values:

    • +,-,* and /
  • Use the given and when statements to perform:

    • addition
    • subtraction
    • multiplication
    • division

Sample input

$num1 = 5.5
$num2 = 6.5
$Operator = '+'

Sample output

Output = 12

Coding exercise

Write your code below. It is recommended that you try solving the exercise yourself before viewing the solution.

Good Luck!

Perl
#you are given variables $num1, $num2 containing some values
#and an $Operator variable that contains the operator that is to be applied to $num1 and $num2
#temp will contain the final answer after the required operation is performed
$temp=-76575;
#write your code for given and when statements here