PHP Increasing Number Triangle Pattern (Right-Aligned)

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

What Is This Pattern?

A right-aligned number triangle prints 1 to $i on row $i, with leading spaces so shorter rows sit against the right edge.

Remember
Rule: spaces while j > i, then print 1..i with width 2

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

In PHP print one space while $j > $i, then printf("%2d", $k) for $k = 1..$i, then echo PHP_EOL.

How to Solve It

One outer row loop, a space loop for indent, then an ascending number loop.

MethodIdeaBest for
Right-alignedSpaces while $j > $i, then %2d for 1..$iLearning, interviews, exams
Rows inputSame logic with a user-chosen $rowsPractice / demos

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from rows down to i+1:
        print one space
    for k from 1 to i:
        print k (width 2)
    print newline

Cheat sheet

GoalPattern
Walk rowsfor ($i = 1; $i <= $rows; $i++)
Leading spacesfor ($j = $rows; $j > $i; $j--) echo " ";
Print 1..$ifor ($k = 1; $k <= $i; $k++) printf("%2d", $k);
End the rowecho PHP_EOL;

Printing Numbers vs Starting a New Line

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

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

Live Preview

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

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

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

Worked Walkthrough — $rows = 4

Trace spaces and the ascending sequence on each row.

$iSpaces / numbersPrinted row
13 spaces / 11
22 spaces / 1 21 2
31 space / 1 2 31 2 3
4none / 1 2 3 41 2 3 4

Space count = $rows - $i. Total numbers for n rows = n(n+1)/2.

PHP Programs

Three complete programs: fixed 5 rows, CLI input rows, and a left-aligned contrast. Use View Output for sample results.

Example 1 — Fixed $rows = 5

Space loop for indent, then print 1..$i with width 2.

PHP
<?php
$rows = 5;

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

    for ($k = 1; $k <= $i; $k++)
        printf("%2d", $k);

    echo PHP_EOL;
}

How It Works

1. Outer loop. $i runs from 1 to 5 — one row per value of $i.

2. Indent. Print one space while $j runs from $rows down past $i.

3. Numbers. Print $k from 1 to $i with printf("%2d", $k), then end the line.

Example 2 — User Input (rows)

Read the row count from STDIN and build the same right-aligned triangle.

PHP
<?php
echo "Enter number of 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 = 1; $i <= $rows; $i++) {
    for ($j = $rows; $j > $i; $j--)
        echo " ";

    for ($k = 1; $k <= $i; $k++)
        printf("%2d", $k);

    echo PHP_EOL;
}

How It Works

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

2. Same triangle. Only the bound changes from the literal 5 to the user value.

Example 3 — Left-Aligned Contrast

Skip the space loop — numbers start at the left margin.

PHP
<?php
$rows = 5;

for ($i = 1; $i <= $rows; $i++) {
    for ($k = 1; $k <= $i; $k++)
        printf("%2d", $k);

    echo PHP_EOL;
}

How It Works

1. No indent. Without the space loop, every row starts at column zero.

2. Same numbers. %2d still pads each digit so columns stay even.

Edge Cases & Pitfalls

Check these before calling the solution done.

no spaces

Forget the space loop

The triangle snaps left. Keep for ($j = $rows; $j > $i; $j--) for right alignment.

no format

Print $k without width

Columns drift once values hit two digits. Prefer printf("%2d", $k).

PHP_EOL early

echo PHP_EOL inside the number loop

That puts every digit on its own line. End the row only after both inner loops.

rows = 1

Single row

Output is just the formatted 1 — no leading spaces.

two spaces

Indent with " " while using %2d

That over-indents. One space per missing number matches the format width better.

Bad input

(int) alone is not enough

Prefer is_numeric after trim(fgets(STDIN)) so non-numeric input does not silently become 0.

Time and Space Complexity

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

Total printed numbers are 1 + 2 + … + n = n(n+1)/2, so work is quadratic in the row count.

Key Takeaways

  • Rule: spaces while $j > $i, then print 1..$i with %2d.
  • Align: space count = $rows - $i; width-2 keeps digits in columns.
  • echo vs PHP_EOL: spaces and numbers stay on the line; echo PHP_EOL advances after each row.
  • Next step: Program 44 prints a centered number diamond.

One line: indent with spaces, then print 1 to $i with fixed width on each row.

Frequently Asked Questions

A right-aligned ascending triangle: row 1 prints 1, row 2 prints 1 2, row 3 prints 1 2 3, and so on.
Before printing numbers, the program prints spaces while $j > $i. Smaller rows get more spaces, pushing digits toward the right edge.
It reserves 2 columns per number (right-aligned), so columns stay stable when values become two digits.
Remove the space loop that prints leading spaces, then print numbers 1 to $i directly — see Example 3.
Program 42 prints a hollow square with border conditions. Program 43 prints an ascending number triangle with indentation spaces.
Use a $rows variable and loop $i from 1 to $rows — see Example 2.
O(n²) for n rows because total prints 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 formatted 1 with no leading spaces.

Did you know?

Each row prints numbers 1 to $i. A space loop runs while $j > $i before the number loop; %2d keeps columns aligned. Total prints = n(n+1)/2.

Next: Number Diamond Pattern

Print a centered diamond of ascending digit sequences.

Program 44 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

6 people found this page helpful