0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Print Number Pattern Example

Use a nested loop in PowerShell like for ($i=1; $i -le 5; $i++) { for ($j=1; $j -le $i; $j++) { Write-Host -NoNewline $j } Write-Host '' } to print a number pattern.
📋

Examples

Input5
Output1 12 123 1234 12345
Input3
Output1 12 123
Input1
Output1
🧠

How to Think About It

To print a number pattern, think of rows and columns. For each row from 1 to the input number, print numbers starting from 1 up to that row number. Use nested loops where the outer loop controls rows and the inner loop prints numbers in each row.
📐

Algorithm

1
Get the input number for the pattern size.
2
Start an outer loop from 1 to the input number for rows.
3
Inside the outer loop, start an inner loop from 1 to the current row number.
4
Print the inner loop number without moving to a new line.
5
After the inner loop ends, move to a new line.
6
Repeat until all rows are printed.
💻

Code

powershell
param([int]$n = 5)
for ($i = 1; $i -le $n; $i++) {
    for ($j = 1; $j -le $i; $j++) {
        Write-Host -NoNewline $j
    }
    Write-Host ""
}
Output
1 12 123 1234 12345
🔍

Dry Run

Let's trace input 3 through the code

1

Outer loop i=1

Inner loop j runs from 1 to 1, prints '1'

2

Outer loop i=2

Inner loop j runs from 1 to 2, prints '12'

3

Outer loop i=3

Inner loop j runs from 1 to 3, prints '123'

iPrinted Numbers
11
212
3123
💡

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from 1 to the input number, deciding how many rows to print.

Step 2: Inner loop prints numbers

The inner for loop prints numbers from 1 up to the current row number without moving to a new line.

Step 3: New line after each row

After printing each row, Write-Host "" moves the cursor to the next line to start a new row.

🔄

Alternative Approaches

Using while loops
powershell
param([int]$n = 5)
$i = 1
while ($i -le $n) {
    $j = 1
    while ($j -le $i) {
        Write-Host -NoNewline $j
        $j++
    }
    Write-Host ""
    $i++
}
While loops achieve the same result but are less concise than for loops.
Using string multiplication and substring
powershell
param([int]$n = 5)
for ($i = 1; $i -le $n; $i++) {
    $line = -join (1..$n)
    Write-Host $line.Substring(0, $i)
}
This method builds a full string and prints substrings, which can be simpler for some patterns.

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

Time Complexity

The nested loops run roughly n*(n+1)/2 times, which is O(n^2), because for each row i, it prints i numbers.

Space Complexity

The script uses constant extra space, only loop counters and temporary variables, so O(1).

Which Approach is Fastest?

Both for and while loops have similar performance; string manipulation may be slightly slower but simpler for some patterns.

ApproachTimeSpaceBest For
For loopsO(n^2)O(1)Clear and concise control of loops
While loopsO(n^2)O(1)More flexible loop control
String substringO(n^2)O(1)Simpler code for fixed patterns
💡
Use -NoNewline with Write-Host to print on the same line.
⚠️
Forgetting to move to a new line after each row causes all numbers to print on one line.