Shape Rule
Growing rows
Row $i prints numbers 1..$i; leading spaces right-align the triangle.

The right-aligned increasing number triangle prints 1, then 1 2, then 1 2 3, and so on — with leading spaces so each row aligns on the right. This tutorial covers the space loop, number loop, live preview, algorithm steps, worked PHP examples, edge cases, and complexity.
Growing rows
Row $i prints numbers 1..$i; leading spaces right-align the triangle.
$j = $rows..$i+1
for ($j = $rows; $j > $i; $j--) prints $rows - $i leading spaces.
1..$i per row
printf("%2d", $k) prints $k = 1..$i on every row.
Row index i
for ($i = 1; $i <= $rows; $i++) walks each row of the triangle.
1–20 rows
Pick a row count and draw the right-aligned increasing number triangle instantly in the browser.
Complexity
Prints n(n+1)/2 numbers across n rows — n² iterations; extra memory stays O(1).
A right-aligned increasing number triangle prints 1, then 1 2, then 1 2 3, up to $rows. With $rows = 5, the widest row sits flush right and shorter rows indent from the left.
In PHP you use an outer loop for each row, a space loop that prints $rows - $i spaces, and a number loop that prints printf("%2d", $k) for $k = 1..$i.
It teaches indent-then-content alignment — the same trick used for pyramids, diamonds, and right-aligned star patterns.
Space loop first, then number loop — both run on every row.
$rows - $i spaces shrink each row toward the right edge.
Two-character fields keep columns aligned when numbers reach two digits.
Follow Program 42 hollow square; continue to Program 44 number diamond.
In short: for each row $i, print $rows - $i spaces, then print numbers 1..$i with %2d, then call echo PHP_EOL.
Given a positive integer $rows (e.g. 5), print a right-aligned triangle: row i shows numbers 1..$i with leading spaces.
// $rows = 5 (conceptual shape)
// 1
// 1 2
// 1 2 3
// 1 2 3 4
// 1 2 3 4 5 | Item | Type | Description |
|---|---|---|
$rows | int | Number of triangle rows to print (typically ≥ 1). |
| Printed output | text | Right-aligned rows of increasing numbers; row i has i values. |
for $i from 1 to $rows:
print ($rows - $i) spaces
for $k from 1 to $i: print k with %2d
print newline | Approach | Idea | Best for |
|---|---|---|
| Space + number loops | Leading spaces, then 1..$i with %2d | Learning and interviews |
| User-input size | (int) $input; | Flexible console programs |
| Left-aligned triangle | Skip the space loop | Contrast with right alignment |
| Goal | Pattern |
|---|---|
| Walk rows | for ($i = 1; $i <= $rows; $i++) |
| Print spaces | for ($j = $rows; $j > $i; $j--) |
| Print numbers | for ($k = 1; $k <= $i; $k++) + %2d |
| End the row | echo PHP_EOL; |
| Program 42 contrast | Hollow square uses a border if; this pattern uses spaces + increasing numbers |
Same triangle row — how leading spaces and numbers work together.
$j = $rows; $j > $i; $j--Prints $rows - $i leading spaces
$k = 1..$iPrints printf("%2d", $k)
i numbersRow i always prints i numbers
trace i=3Dry-run row 3: two spaces then 1 2 3
Reach for this pattern when teaching indent-then-content alignment with nested loops.
Classic follow-up after hollow squares and centered pyramids.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible row count.
Compare with Program 42 (hollow square), then continue to Program 44 (number diamond).
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a row count between 1 and 20 and draw the right-aligned increasing number triangle in the browser.
Three complete PHP programs — fixed row count, fgets(STDIN) input, and a left-aligned variant. Click View Output to reveal sample console results.
Print five rows with a space loop and number loop per row.
$rows = 5Hard-coded size — nested loops and a space loop build each row.
<?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;
} When $i = 1, the space loop prints four spaces, then one formatted 1. When $i = 3, two spaces precede 1 2 3 — the triangle grows downward and right.
Let the user choose the height at runtime.
Read the row count with trim(fgets(STDIN)) (check is_numeric($input) in real apps).
<?php
echo "Enter the number of rows: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input)) {
echo "Invalid input." . 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;
} Same nested-loop core as Example 1; only the source of $rows changes. Non-numeric input fails is_numeric($input) — validate before casting to int for safer labs.
Remove the space loop to print a completely left-aligned triangle.
Skip the space loop so numbers start at the left margin — a common contrast exercise.
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($k = 1; $k <= $i; $k++) {
printf("%2d", $k);
}
echo PHP_EOL;
} Same number loop; removing the leading-space loop leaves rows flush left.
echo is built in; use fgets(STDIN) when reading input. Set $rows (fixed or from input).
for ($i = 1; $i <= $rows; $i++) — walks each row of the triangle.
for ($j = $rows; $j > $i; $j--) prints $rows - $i leading spaces.
printf("%2d", $k) for $k = 1..$i, then echo PHP_EOL ends the row.
Total numbers printed: n(n+1)/2 — O(n²) time, O(1) extra memory.
$rows = 5, row $i = 3Trace one middle row to see spaces and numbers combine.
| Step | Loop | Prints |
|---|---|---|
| Spaces | $j = 5, 4 (2 times) | |
| Numbers | $k = 1 | 1 |
| Numbers | $k = 2 | 2 |
| Numbers | $k = 3 | 3 |
Row output: 1 2 3 — total numbers for full triangle: 1+2+3+4+5 = 15 = n(n+1)/2.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: remove the space loop and watch rows snap left.
Foundation for number diamonds, centered pyramids, and mirrored patterns.
Example: mirror rows downward to build Program 44’s diamond.
Practice echo vs row newline without complex math.
Example: put echo PHP_EOL inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: print 1, 12, 123 without spaces between digits.
Triangular totals make O(n²) concrete for beginners.
Example: count printed numbers for n = 5 → 15.
Pair the pattern with fgets(STDIN) and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner PHP courses.
Wrong bounds show up immediately as a misaligned or left-aligned shape.
Only loops and console output — no arrays or math libraries.
Invert, center, left-align, or swap digits for stars with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the space-loop version first; then try the left-aligned triangle in Example 3.
Small habits that keep number-pattern code clean.
Use $rows (or n) and keep i/j for row/column — or rename to row/col.
fgets(STDIN)Avoid crashes when the user types letters instead of a number.
Only call echo PHP_EOL after the inner loop finishes the row.
One-liner for spaces: echo " ".repeat($rows - $i)); before the number loop.
Trace $rows = 3 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of digits per line, you almost certainly put echo PHP_EOL inside the inner loop.
Mistakes that commonly break right-aligned number triangles.
Each number lands on its own line — you get a column, not a triangle.
→ Use echo for spaces and numbers; echo PHP_EOL only after both inner loops.
Using $j = 1..$i for spaces under-indents rows and breaks right alignment.
→ Keep for ($j = $rows; $j > $i; $j--) to print $rows-$i leading spaces.
Omitting echo PHP_EOL glues every row onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input leave $rows unset.
→ Use is_numeric($input) and re-prompt on failure.
Printing bare $k without %2d breaks alignment once values reach two digits.
→ Use printf("%2d", $k) so columns stay aligned.
Check these inputs before calling the solution done.
Output is one indented 1 when right-aligned.
Outer loop never runs — print nothing or show a message.
$rows < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as n(n+1)/2 numbers plus spaces — fine for labs, noisy for huge n.
Unchecked fgets(STDIN) leaves $rows unset — call is_numeric($input) first.
Remove the space loop — see Example 3.
Try these variations to lock in the pattern.
i from $rows down to 11, 12, 123 without spacesn(n+1)/2; leading spaces add roughly the same order of characters.echo stays on the line; echo PHP_EOL advances — mix them carefully.$rows > 0 for interactive programs; $rows = 1 should print one indented 1.printf("%2d", $k) so columns stay aligned as values grow.Quick Takeaway: space loop for alignment, number loop for 1..$i, %2d formatting, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
| Left-aligned triangle (Example 3) | O(rows²) | O(1) |
The right-aligned increasing number triangle combines nested loops with a simple alignment pattern — a natural step after hollow squares and pyramids. Master the fixed-$rows version first, then try user input and the left-aligned triangle.
Practice the three examples above, then continue to Program 44 for the number diamond pattern.
Every row prints i numbers — keep echo PHP_EOL only after both inner loops finish.
%2d and echo PHP_EOL after each row$rows ≥ 1 for interactive programsfgets(STDIN) return value before using $rowsecho PHP_EOL inside the inner digit loop%2d needs two$rows = 1 edge casePrint the pattern the beginner-friendly way.
Row i prints 1..$i
Definition$rows - $i spaces
CodePrint 1..$i with %2d
Logicn(n+1)/2 numbers
I/OO(n²) time
AnalysisEach row prints numbers 1..$i and leading spaces push the row to the right — fewer spaces as $i grows, which creates the right-aligned triangle.
Move on to the centered number diamond pattern in the PHP number-pattern series.
12 people found this page helpful