0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Find Factorial of a Number

Use a loop in PowerShell like $factorial = 1; for ($i = 1; $i -le $n; $i++) { $factorial *= $i } to calculate the factorial of a number stored in $n.
📋

Examples

Input0
OutputFactorial of 0 is 1
Input5
OutputFactorial of 5 is 120
Input7
OutputFactorial of 7 is 5040
🧠

How to Think About It

To find the factorial, start with 1 and multiply it by every number from 1 up to the given number. This way, you build the product step by step until you reach the number itself.
📐

Algorithm

1
Get the input number.
2
Initialize a variable to 1 to hold the factorial result.
3
Loop from 1 to the input number.
4
Multiply the result variable by the current loop number each time.
5
After the loop ends, return or print the factorial result.
💻

Code

powershell
$n = Read-Host 'Enter a non-negative integer'
$factorial = 1
for ($i = 1; $i -le $n; $i++) {
    $factorial *= $i
}
Write-Output "Factorial of $n is $factorial"
Output
Enter a non-negative integer: 5 Factorial of 5 is 120
🔍

Dry Run

Let's trace the input 5 through the code

1

Initialize

$factorial = 1, $i = 1

2

First loop iteration

$factorial = 1 * 1 = 1

3

Second loop iteration

$factorial = 1 * 2 = 2

4

Third loop iteration

$factorial = 2 * 3 = 6

5

Fourth loop iteration

$factorial = 6 * 4 = 24

6

Fifth loop iteration

$factorial = 24 * 5 = 120

IterationValue of iFactorial
111
222
336
4424
55120
💡

Why This Works

Step 1: Start with 1

We set $factorial to 1 because multiplying by 1 does not change the value and it is the identity for multiplication.

Step 2: Multiply step by step

Each loop multiplies $factorial by the current number $i, building the factorial product gradually.

Step 3: Loop until input number

The loop runs until it reaches the input number, ensuring all numbers from 1 to $n are included in the multiplication.

🔄

Alternative Approaches

Recursive function
powershell
function Get-Factorial($n) {
    if ($n -le 1) { return 1 }
    else { return $n * (Get-Factorial ($n - 1)) }
}
$n = Read-Host 'Enter a non-negative integer'
$result = Get-Factorial $n
Write-Output "Factorial of $n is $result"
Uses recursion which is elegant but can cause stack overflow for very large numbers.
Using .NET BigInteger for large numbers
powershell
Add-Type -AssemblyName System.Numerics
function Get-FactorialBig([int]$n) {
    $result = [System.Numerics.BigInteger]::One
    for ($i = 1; $i -le $n; $i++) {
        $result = $result * $i
    }
    return $result
}
$n = Read-Host 'Enter a non-negative integer'
$result = Get-FactorialBig $n
Write-Output "Factorial of $n is $result"
Handles very large factorials without overflow but requires .NET BigInteger.

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

Time Complexity

The loop runs from 1 to n, so the time grows linearly with the input number.

Space Complexity

Only a few variables are used, so space stays constant regardless of input size.

Which Approach is Fastest?

The iterative loop is fastest and uses less memory than recursion, which adds call stack overhead.

ApproachTimeSpaceBest For
Iterative loopO(n)O(1)General use, efficient
Recursive functionO(n)O(n)Elegant code, small inputs
.NET BigInteger loopO(n)O(1)Very large numbers
💡
Always check that the input is a non-negative integer before calculating factorial.
⚠️
Forgetting to initialize the factorial variable to 1 causes incorrect results.