PHP Decreasing Number Triangle Pattern (Right-Aligned)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A right-aligned decreasing triangle indents each row, then prints values from $rows down to the current $i — so every row starts at the same high number.

Remember
Rule: indent (i-1) times, then print rows..i in width 2

         5
       5 4
     5 4 3
   5 4 3 2
 5 4 3 2 1   ← rows = 5

In PHP the outer loop runs $i from $rows down to 1. First print indentation spaces, then print the decreasing sequence with printf("%2d", $j).

How to Solve It

One descending outer loop with a separate indent loop and number loop.

MethodIdeaBest for
Indent + descendingPad left; print $rows..$i with %2dLearning, interviews, exams
User-input rowsSame logic with a variable heightPractice / demos

Pseudocode

Pseudocode
for i from rows down to 1:
    for j from 1 to i-1:
        print two spaces
    for j from rows down to i:
        print j (width 2)
    print newline

Cheat sheet

GoalPattern
Shrink the row limitfor ($i = $rows; $i >= 1; $i--)
Right-alignfor ($j = 1; $j < $i; $j++) echo " ";
Descending valuesfor ($j = $rows; $j >= $i; $j--)
Fixed-width digitprintf("%2d", $j);
End the rowecho PHP_EOL;

Printing Numbers vs Starting a New Line

APIEffectUse for
echo " " / printf("%2d", $j)Stays on the same lineEach indent or number
echo PHP_EOLEnds the current lineAfter both inner loops

Print indents and numbers without a newline, then end the row once.

Live Preview

Change the row count and the right-aligned decreasing triangle updates instantly.

Whole numbers from 1 to 9 (keeps digits single-width). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 values
         5
       5 4
     5 4 3
   5 4 3 2
 5 4 3 2 1

Worked Walkthrough — $rows = 4

Trace indentation vs the decreasing sequence. Outer $i starts at 4 and shrinks.

$iIndents / numbersPrinted row
43 pads / 44
32 pads / 4 34 3
21 pad / 4 3 24 3 2
1none / 4..14 3 2 1

Larger $i means more indentation and fewer numbers — that is what creates the right-aligned tip at the top.

PHP Programs

Three complete programs: fixed height, CLI input, and a compact 3-row demo. Use View Output for sample results.

Example 1 — Fixed $rows = 5

Descending outer loop: indent, then print $rows..$i with width-2 formatting.

PHP
<?php
$rows = 5;

for ($i = $rows; $i >= 1; $i--) {
    for ($j = 1; $j < $i; $j++)
        echo "  ";

    for ($j = $rows; $j >= $i; $j--)
        printf("%2d", $j);

    echo PHP_EOL;
}

How It Works

1. Outer loop. $i starts at 5 and shrinks to 1 — more numbers appear each row.

2. Indent. Print " " while $j < $i so shorter rows stay right-aligned.

3. Numbers. Print $j from $rows down to $i with printf("%2d", $j) for fixed columns.

Example 2 — User Input

Read the row count from STDIN; both the indent and number loops use $rows.

PHP
<?php
echo "Enter rows: ";
$input = trim(fgets(STDIN));

if (!is_numeric($input) || (int) $input < 1) {
    echo "Please enter a positive whole number." . PHP_EOL;
    exit(1);
}

$rows = (int) $input;

for ($i = $rows; $i >= 1; $i--) {
    for ($j = 1; $j < $i; $j++)
        echo "  ";

    for ($j = $rows; $j >= $i; $j--)
        printf("%2d", $j);

    echo PHP_EOL;
}

How It Works

1. Validate rows. Check is_numeric; require $rows >= 1.

2. Same alignment. Three rows start each line at 3 and grow down to 3 2 1.

Example 3 — Compact $rows = 3

A smaller fixed demo — same indent-and-descend idea, easier to trace by hand.

PHP
<?php
$rows = 3;

for ($i = $rows; $i >= 1; $i--) {
    for ($j = 1; $j < $i; $j++)
        echo "  ";

    for ($j = $rows; $j >= $i; $j--)
        printf("%2d", $j);

    echo PHP_EOL;
}

How It Works

1. Same rules. Indent $i - 1 times; print $rows down to $i.

2. Full width. The last row has no indentation — just 3 2 1.

Edge Cases & Pitfalls

Check these before calling the solution done.

skip indent

Omit the indentation loop

Without for ($j = 1; $j < $i; $j++), every row starts at the left margin.

ascending i

Loop $i from 1 up instead of down

The tip and base swap order. Keep for ($i = $rows; $i >= 1; $i--).

no format

Use echo $j without width

Columns drift for multi-digit values. Prefer printf("%2d", $j).

rows = 1

Single 1

Output is just the formatted 1 — no indentation.

rows ≤ 0

Empty output

The outer loop never runs. Validate and prompt again for clearer UX.

Bad input

Unchecked cast

Always check is_numeric before casting so non-numeric input does not silently become 0.

Time and Space Complexity

ProgramTimeExtra space
Fixed / compact (Examples 1, 3)O(n²)O(1)
User input (Example 2)O(n²)O(1)

Indent steps plus printed numbers across all rows sum to roughly n² work.

Key Takeaways

  • Rule: indent $i - 1 times, then print $rows..$i with width 2.
  • Outer direction: $i runs from $rows down to 1 so the tip appears first.
  • echo vs PHP_EOL: indents and numbers stay on the line; echo PHP_EOL advances after both loops.
  • Next step: Program 37 prints a palindrome number triangle (1, 212, 32123…).

One line: for each $i from $rows down to 1, indent then print the decreasing sequence $rows..$i.

Frequently Asked Questions

A right-aligned decreasing triangle: row 1 prints 5, row 2 prints 5 4, row 3 prints 5 4 3, and so on until 5 4 3 2 1.
An indentation loop prints two spaces while $j < $i before the number loop runs, pushing shorter rows to the right.
The format reserves 2 columns per number (right-aligned), keeping columns stable in the console output.
The number loop runs for ($j = $rows; $j >= $i; $j--), so every row begins at $rows and counts down to the current $i.
Program 35 uses a continuous counter. Program 36 restarts from $rows on each row with a separate indentation loop.
Replace 5 with $rows in the outer loop bound — see Example 2.
O(n²) for n rows because total printed numbers are 1 + 2 + ... + n = n(n+1)/2.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.
Only one row prints — a single number with no leading indentation.

Did you know?

Each row starts from $rows and counts down to $i. An indentation loop prints two spaces per step ($j = 1..$i-1), then printf("%2d", $j) keeps number columns aligned.

Next: Palindrome Number Triangle

Build rows that read the same forward and backward (1, 212, 32123…).

Program 37 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful