PHP Increasing Number Triangle Pattern (Right-Aligned)

Definition
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.
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.
Approach
How to Solve It
One outer row loop, a space loop for indent, then an ascending number loop.
| Method | Idea | Best for |
|---|
| Right-aligned | Spaces while $j > $i, then %2d for 1..$i | Learning, interviews, exams |
| Rows input | Same logic with a user-chosen $rows | Practice / demos |
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
| Goal | Pattern |
|---|
| Walk rows | for ($i = 1; $i <= $rows; $i++) |
| Leading spaces | for ($j = $rows; $j > $i; $j--) echo " "; |
| Print 1..$i | for ($k = 1; $k <= $i; $k++) printf("%2d", $k); |
| End the row | echo PHP_EOL; |
Printing Numbers vs Starting a New Line
| API | Effect | Use for |
|---|
echo " " / printf("%2d", $k) | Stays on the same line | Each space or number |
echo PHP_EOL | Ends the current line | After both inner loops |
Print spaces and numbers without a newline, then end the row once.
Try it
Live Preview
Change the row count and the right-aligned triangle updates instantly.
Trace
Worked Walkthrough — $rows = 4
Trace spaces and the ascending sequence on each row.
$i | Spaces / numbers | Printed row |
|---|
1 | 3 spaces / 1 | 1 |
2 | 2 spaces / 1 2 | 1 2 |
3 | 1 space / 1 2 3 | 1 2 3 |
4 | none / 1 2 3 4 | 1 2 3 4 |
Space count = $rows - $i. Total numbers for n rows = n(n+1)/2.
Code
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
$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;
}
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5How 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
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
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($k = 1; $k <= $i; $k++)
printf("%2d", $k);
echo PHP_EOL;
}
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
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 spacesForget the space loop
The triangle snaps left. Keep for ($j = $rows; $j > $i; $j--) for right alignment.
no formatPrint $k without width
Columns drift once values hit two digits. Prefer printf("%2d", $k).
PHP_EOL earlyecho PHP_EOL inside the number loop
That puts every digit on its own line. End the row only after both inner loops.
rows = 1Single row
Output is just the formatted 1 — no leading spaces.
two spacesIndent 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.
Analysis
Time and Space Complexity
| Program | Time | Extra 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.
Remember
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.
About the author
Developer, cloud engineer, and technical writer
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