0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Find Largest of Three Numbers

Use the PowerShell code if ($a -ge $b -and $a -ge $c) { $a } elseif ($b -ge $a -and $b -ge $c) { $b } else { $c } to find the largest of three numbers stored in variables $a, $b, and $c.
📋

Examples

Inputa=5, b=3, c=9
Output9
Inputa=10, b=10, c=2
Output10
Inputa=-1, b=-5, c=-3
Output-1
🧠

How to Think About It

To find the largest number among three, compare the first number with the other two using -ge (greater or equal) operator. If it is greater or equal to both, it is the largest. Otherwise, compare the second number similarly. If neither the first nor second is largest, the third must be the largest.
📐

Algorithm

1
Get the three numbers as input.
2
Check if the first number is greater than or equal to both the second and third numbers.
3
If yes, return the first number as largest.
4
Else, check if the second number is greater than or equal to both the first and third numbers.
5
If yes, return the second number as largest.
6
Otherwise, return the third number as largest.
💻

Code

powershell
$a = 5
$b = 3
$c = 9

if ($a -ge $b -and $a -ge $c) {
    Write-Output $a
} elseif ($b -ge $a -and $b -ge $c) {
    Write-Output $b
} else {
    Write-Output $c
}
Output
9
🔍

Dry Run

Let's trace the example where a=5, b=3, c=9 through the code

1

Compare a with b and c

Check if 5 >= 3 and 5 >= 9 → false because 5 is not >= 9

2

Compare b with a and c

Check if 3 >= 5 and 3 >= 9 → false because 3 is not >= 5 or 9

3

Return c as largest

Since previous checks failed, output 9

StepConditionResult
15 >= 3 and 5 >= 9False
23 >= 5 and 3 >= 9False
3Return 99
💡

Why This Works

Step 1: Compare first number

The code uses -ge to check if the first number is greater or equal to the other two, ensuring it is the largest if true.

Step 2: Compare second number

If the first number is not largest, the second number is checked similarly to find if it is the largest.

Step 3: Default to third number

If neither first nor second is largest, the third number is returned as the largest by default.

🔄

Alternative Approaches

Using PowerShell's [Math]::Max method
powershell
$a = 5
$b = 3
$c = 9
$max = [Math]::Max($a, [Math]::Max($b, $c))
Write-Output $max
This approach is shorter and uses built-in .NET method for maximum, improving readability.
Using array and Sort-Object
powershell
$numbers = @(5, 3, 9)
$largest = $numbers | Sort-Object -Descending | Select-Object -First 1
Write-Output $largest
This method handles any number of inputs but is less efficient for just three numbers.

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

Time Complexity

The script performs a fixed number of comparisons (at most two), so it runs in constant time O(1).

Space Complexity

Only a few variables are used to store inputs and results, so space complexity is O(1).

Which Approach is Fastest?

Direct comparisons are fastest for three numbers. Using [Math]::Max is also efficient and concise. Sorting is slower and better for larger lists.

ApproachTimeSpaceBest For
Direct comparisonsO(1)O(1)Small fixed number of inputs
[Math]::Max methodO(1)O(1)Concise code for few inputs
Sort-Object arrayO(n log n)O(n)Finding max in larger lists
💡
Use [Math]::Max for a concise and efficient way to find the largest number in PowerShell.
⚠️
Beginners often forget to use -ge (greater or equal) and use -gt (greater than) which can cause issues when numbers are equal.