0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Print Diamond Pattern

Use a nested loop in PowerShell to print spaces and stars in increasing and then decreasing order like for ($i=1; $i -le $n; $i++) { ... } and for ($i=$n-1; $i -ge 1; $i--) { ... } to form a diamond pattern.
📋

Examples

Input3
Output * *** ***** *** *
Input5
Output * *** ***** ******* ********* ******* ***** *** *
Input1
Output*
🧠

How to Think About It

To print a diamond, first print the top half with increasing stars centered by spaces, then print the bottom half with decreasing stars similarly. Each line has spaces to center stars, and stars increase by 2 each line.
📐

Algorithm

1
Get input number n for diamond size
2
For each line i from 1 to n, print (n - i) spaces and (2*i - 1) stars
3
For each line i from n-1 down to 1, print (n - i) spaces and (2*i - 1) stars
4
End
💻

Code

powershell
param([int]$n = 5)
for ($i = 1; $i -le $n; $i++) {
    $spaces = ' ' * ($n - $i)
    $stars = '*' * (2 * $i - 1)
    Write-Output "$spaces$stars"
}
for ($i = $n - 1; $i -ge 1; $i--) {
    $spaces = ' ' * ($n - $i)
    $stars = '*' * (2 * $i - 1)
    Write-Output "$spaces$stars"
}
🔍

Dry Run

Let's trace the diamond pattern for input n=3 through the code

1

Top half line 1

spaces = 3 - 1 = 2 spaces, stars = 2*1 - 1 = 1 star -> ' *'

2

Top half line 2

spaces = 3 - 2 = 1 space, stars = 3 stars -> ' ***'

3

Top half line 3

spaces = 3 - 3 = 0 spaces, stars = 5 stars -> '*****'

4

Bottom half line 1

spaces = 3 - 2 = 1 space, stars = 3 stars -> ' ***'

5

Bottom half line 2

spaces = 3 - 1 = 2 spaces, stars = 1 star -> ' *'

LineSpacesStarsOutput
121 *
213 ***
305*****
413 ***
521 *
💡

Why This Works

Step 1: Calculate spaces

Each line has spaces equal to n - i to center the stars.

Step 2: Calculate stars

Stars increase by 2 each line using 2 * i - 1 to keep the diamond shape.

Step 3: Print top and bottom halves

First print lines from 1 to n, then from n-1 down to 1 to complete the diamond.

🔄

Alternative Approaches

Using single loop with conditional
powershell
param([int]$n = 5)
for ($i = 1; $i -lt 2 * $n; $i++) {
    $line = $i -le $n ? $i : 2 * $n - $i
    $spaces = ' ' * ($n - $line)
    $stars = '*' * (2 * $line - 1)
    Write-Output "$spaces$stars"
}
Uses one loop and conditional to reduce code size but may be less clear.
Using arrays to build lines first
powershell
param([int]$n = 5)
$lines = @()
for ($i = 1; $i -le $n; $i++) {
    $spaces = ' ' * ($n - $i)
    $stars = '*' * (2 * $i - 1)
    $lines += "$spaces$stars"
}
$lines += $lines[0..($n-2)] | Sort-Object -Descending
$lines | ForEach-Object { Write-Output $_ }
Builds all lines first, then prints; useful if you want to reuse or modify lines.

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

Time Complexity

The script runs two loops each up to n, and each line prints up to 2*n characters, so time is O(n^2).

Space Complexity

Uses only a few variables and prints lines directly, so space is O(n) for output lines if stored.

Which Approach is Fastest?

The single loop with conditional is slightly faster due to fewer loops but less readable than two separate loops.

ApproachTimeSpaceBest For
Two loops (main)O(n^2)O(n)Clarity and simplicity
Single loop with conditionalO(n^2)O(n)Compact code, slight speed gain
Build lines in arrayO(n^2)O(n)Reuse or modify lines before printing
💡
Use string multiplication like ' ' * count and '*' * count to easily create spaces and stars.
⚠️
Beginners often forget to adjust spaces, causing the diamond to be left-aligned instead of centered.