0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Create Simple Calculator

Use a PowerShell script that reads two numbers and an operator with Read-Host, then uses switch to perform the calculation, like: $result = switch ($operator) { '+' { $num1 + $num2 } '-' { $num1 - $num2 } '*' { $num1 * $num2 } '/' { if ($num2 -eq 0) { 'Cannot divide by zero.' } else { $num1 / $num2 } } }.
📋

Examples

Input5, 3, +
OutputResult: 8
Input10, 2, /
OutputResult: 5
Input7, 0, /
OutputCannot divide by zero.
🧠

How to Think About It

To build a simple calculator, first get two numbers and an operator from the user. Then, decide which math operation to do based on the operator using a switch or if statement. Finally, show the result or handle errors like division by zero.
📐

Algorithm

1
Prompt the user to enter the first number.
2
Prompt the user to enter the second number.
3
Prompt the user to enter an operator (+, -, *, /).
4
Check the operator and perform the corresponding calculation.
5
If division is chosen, check if the second number is zero to avoid error.
6
Display the result or an error message.
💻

Code

powershell
$num1 = [double](Read-Host 'Enter first number')
$num2 = [double](Read-Host 'Enter second number')
$operator = Read-Host 'Enter operator (+, -, *, /)'
switch ($operator) {
    '+' { $result = $num1 + $num2 }
    '-' { $result = $num1 - $num2 }
    '*' { $result = $num1 * $num2 }
    '/' {
        if ($num2 -eq 0) {
            Write-Output 'Cannot divide by zero.'
            exit
        } else {
            $result = $num1 / $num2
        }
    }
    default { Write-Output 'Invalid operator.'; exit }
}
Write-Output "Result: $result"
Output
Enter first number: 5 Enter second number: 3 Enter operator (+, -, *, /): + Result: 8
🔍

Dry Run

Let's trace the input 5, 3, + through the code

1

Read first number

$num1 = 5

2

Read second number

$num2 = 3

3

Read operator

$operator = '+'

4

Perform addition

$result = 5 + 3 = 8

5

Output result

Result: 8

StepVariableValue
1$num15
2$num23
3$operator+
4$result8
💡

Why This Works

Step 1: Input reading

The script uses Read-Host to get user input as strings, then converts numbers to [double] for calculations.

Step 2: Operation selection

The switch statement chooses the math operation based on the operator entered.

Step 3: Division check

Before dividing, the script checks if the second number is zero to avoid errors and informs the user.

Step 4: Output

The result is printed with Write-Output, showing the final calculation.

🔄

Alternative Approaches

Using if-else statements
powershell
$num1 = [double](Read-Host 'Enter first number')
$num2 = [double](Read-Host 'Enter second number')
$operator = Read-Host 'Enter operator (+, -, *, /)'
if ($operator -eq '+') { $result = $num1 + $num2 }
elseif ($operator -eq '-') { $result = $num1 - $num2 }
elseif ($operator -eq '*') { $result = $num1 * $num2 }
elseif ($operator -eq '/') {
    if ($num2 -eq 0) { Write-Output 'Cannot divide by zero.'; exit } else { $result = $num1 / $num2 }
} else { Write-Output 'Invalid operator.'; exit }
Write-Output "Result: $result"
This approach uses if-else instead of switch; it is more verbose but familiar to some beginners.
Using a function for calculation
powershell
function Calculate($a, $b, $op) {
    switch ($op) {
        '+' { return $a + $b }
        '-' { return $a - $b }
        '*' { return $a * $b }
        '/' { if ($b -eq 0) { return 'Cannot divide by zero.' } else { return $a / $b } }
        default { return 'Invalid operator.' }
    }
}
$num1 = [double](Read-Host 'Enter first number')
$num2 = [double](Read-Host 'Enter second number')
$operator = Read-Host 'Enter operator (+, -, *, /)'
$result = Calculate $num1 $num2 $operator
Write-Output "Result: $result"
Encapsulates logic in a function for reuse and cleaner main code.

Complexity: O(1) time, O(1) space

Time Complexity

The script performs a fixed number of operations regardless of input size, so it runs in constant time O(1).

Space Complexity

It uses a few variables for input and output, so space usage is constant O(1).

Which Approach is Fastest?

All approaches run in constant time; using a function improves readability but does not affect speed.

ApproachTimeSpaceBest For
Switch statementO(1)O(1)Simple, clear operation selection
If-else statementsO(1)O(1)Beginners familiar with if-else
Function encapsulationO(1)O(1)Reusable and cleaner code
💡
Always convert user input to numbers before calculations to avoid errors.
⚠️
Forgetting to check for division by zero causes runtime errors.