0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Print Right Triangle Pattern

Use a nested loop in PowerShell like for ($i=1; $i -le $n; $i++) { for ($j=1; $j -le $i; $j++) { Write-Host '*' -NoNewline } Write-Host '' } to print a right triangle pattern of stars.
📋

Examples

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

How to Think About It

To print a right triangle pattern, think of printing one star on the first line, two stars on the second, and so on until the given number of lines. Use an outer loop to count lines and an inner loop to print stars equal to the current line number.
📐

Algorithm

1
Get the number of lines (n) from the user.
2
Start a loop from 1 to n for each line.
3
For each line, print stars equal to the current line number.
4
Move to the next line after printing stars.
5
Repeat until all lines 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
    }
    Write-Host ''
}
Output
* ** *** **** *****
🔍

Dry Run

Let's trace printing a right triangle with 3 lines through the code

1

Start outer loop with i=1

Print 1 star: *

2

Outer loop i=2

Print 2 stars: **

3

Outer loop i=3

Print 3 stars: ***

Line (i)Stars Printed
1*
2**
3***
💡

Why This Works

Step 1: Outer loop controls lines

The for loop with variable i runs from 1 to n, each iteration representing one line.

Step 2: Inner loop prints stars

For each line, the inner for loop prints stars equal to the current line number i using Write-Host '*' -NoNewline.

Step 3: Move to next line

After printing stars for a line, Write-Host '' moves the cursor to the next line for the next iteration.

🔄

Alternative Approaches

Using string multiplication
powershell
param([int]$n = 5)
for ($i = 1; $i -le $n; $i++) {
    Write-Host ('*' * $i)
}
This approach uses PowerShell's string multiplication to print stars, making the code shorter and easier to read.
Using while loops
powershell
param([int]$n = 5)
$i = 1
while ($i -le $n) {
    $j = 1
    while ($j -le $i) {
        Write-Host '*' -NoNewline
        $j++
    }
    Write-Host ''
    $i++
}
This uses while loops instead of for loops, which some beginners find easier to understand.

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

Time Complexity

The nested loops run approximately n*(n+1)/2 times, which is O(n^2), because for each line i, i stars are printed.

Space Complexity

The script uses constant extra space, O(1), as it only stores loop counters and prints directly.

Which Approach is Fastest?

Using string multiplication is generally faster and cleaner than nested loops, but both have the same O(n^2) time complexity.

ApproachTimeSpaceBest For
Nested for loopsO(n^2)O(1)Clear step-by-step printing
String multiplicationO(n^2)O(1)Concise and readable code
While loopsO(n^2)O(1)Beginners learning loop basics
💡
Use Write-Host '*' -NoNewline to print stars on the same line without moving to the next line.
⚠️
Forgetting -NoNewline causes each star to print on a new line instead of forming a triangle.