Shape Rule
Right-aligned triangle
Row $i prints $i numbers from counter $k, with leading spaces while $j > $i.

The right-aligned incremental triangle prints 1, then 2 3, then 4 5 6, … — a natural follow-up after Program 34’s zero-based $i + $j triangle. This tutorial covers the continuous counter $k, fixed-width formatting, nested loops, a live preview, worked PHP examples, edge cases, and complexity.
Right-aligned triangle
Row $i prints $i numbers from counter $k, with leading spaces while $j > $i.
$i = 1..$rows
for ($i = 1; $i <= $rows; $i++) — one growing row per iteration.
$rows..1
for ($j = $rows; $j >= 1; $j--) — fixed width; spaces or numbers per column.
%3d format
printf("%3d", $k++) — continuous sequence with fixed-width columns.
3–7 rows
Pick a row count and draw the right-aligned incremental triangle in the browser.
Complexity
Total prints = n(n+1)/2 — work scales as n².
A right-aligned incremental number triangle prints a continuous sequence: 1, then 2 3, then 4 5 6, and so on. With $rows = 5, numbers shift right each row thanks to leading spaces.
In PHP you use nested loops with a counter $k: print " " while $j > $i, otherwise printf("%3d", $k++), then echo PHP_EOL.
It combines nested loops, a running counter, and formatted output — a step after Program 34’s formula-based triangle.
Continuous sequence.
Right alignment.
Fixed-width columns.
Follow Program 34; continue to Program 36 (decreasing) next.
In short: outer $i = 1..$rows, inner $j = $rows..1, spaces while $j > $i, else %3d with $k++, then echo PHP_EOL.
Given $rows = 5, print a right-aligned incremental triangle: counter $k starts at 1, leading spaces while $j > $i, then fixed-width numbers.
// $rows = 5
// 1
// 2 3
// 4 5 6
// 7 8 9 10
//11 12 13 14 15 | Item | Type | Description |
|---|---|---|
$rows | int | Triangle height — number of lines to print. |
$i | int | Outer loop — current row (1 to $rows). |
$j | int | Inner loop — fixed width from $rows down to 1. |
$k | int | Continuous counter — starts at 1, increments per printed number. |
k = 1
for i from 1 to rows:
for j from rows down to 1:
if j > i: print 3 spaces
else: print k in width 3; k++
print newline | Approach | Idea | Best for |
|---|---|---|
| Fixed rows | 1, 2 3, … | Learning and interviews |
| User-input rows | (int) trim(fgets(STDIN)); | Configurable triangle size |
| Compact trace | $rows = 3 on paper first | Debugging loop bounds |
| Goal | Pattern |
|---|---|
| Outer loop | for ($i = 1; $i <= $rows; $i++) |
| Inner loop | for ($j = $rows; $j >= 1; $j--) |
| Leading spaces | if ($j > $i) echo " "; |
| Print number | printf("%3d", $k++); |
| End the row | echo PHP_EOL; |
| User input | (int) trim(fgets(STDIN)); |
Same right-aligned incremental triangle — different ways to control the row count.
$i = 1..$rowsOne growing row per iteration
$k++Continuous sequence
$j = $rows..1Fixed width per row
$j > $iPrint spaces, else number
Reach for this pattern when teaching formatted console output, continuous counters, and right-aligned triangles.
Natural follow-up — adds right alignment and a continuous counter instead of a per-cell formula.
Practice %3d and fixed-width columns before tackling larger patterns.
Combine loops with fgets(STDIN) and is_numeric() checks for flexible row counts.
Compare Program 30 (descending digits) and Program 36 (decreasing sequence) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, formatted output, and O(n²) thinking.
Choose a row count between 3 and 7 and draw the right-aligned incremental triangle in the browser.
Three complete PHP programs — fixed rows, user input, and a smaller trace demo. Click View Output to reveal sample console results.
Print five rows of the right-aligned incremental triangle with counter $k and %3d formatting.
$rows = 5Hard-coded row count — ideal for first demos and screenshots.
<?php
$k = 1;
for ($i = 1; $i <= 5; $i++) {
for ($j = 5; $j >= 1; $j--) {
if ($j > $i) {
echo " ";
} else {
printf("%3d", $k++);
}
}
echo PHP_EOL;
} When $i = 1, the inner loop prints four space groups then 1. When $i = 5, no leading spaces — output 11 12 13 14 15 with fixed-width columns.
Read the row count with fgets(STDIN) instead of hard-coding 5.
Read $rows with (int) trim(fgets(STDIN)); the inner loop uses $rows as the fixed width.
<?php
echo "Enter rows: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input)) return;
$rows = (int) $input;
if ($rows < 1) return;
$k = 1;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $rows; $j >= 1; $j--) {
if ($j > $i) {
echo " ";
} else {
printf("%3d", $k++);
}
}
echo PHP_EOL;
} Same counter and formatting core as Example 1; only $rows comes from user input instead of being hard-coded as 5. Non-numeric input leaves $rows unset if you skip is_numeric() checks — always check it in safer labs.
Run with $rows = 3 to trace every row on paper before scaling up.
$rows = 3Same nested-loop counter with a smaller row count for quick tracing.
<?php
$rows = 3;
$k = 1;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $rows; $j >= 1; $j--) {
if ($j > $i) {
echo " ";
} else {
printf("%3d", $k++);
}
}
echo PHP_EOL;
} Only $rows changes from 5 to 3 — the counter and spacing logic stays identical. Trace $i = 1, 2, 3 on paper to see how leading spaces shrink each row.
Set $k = 1; and $rows = 5;. Use fgets(STDIN) when reading input. Set loop variables $i, $j.
for ($i = 1; $i <= $rows; $i++) — ascending outer loop; one right-aligned row per iteration.
for ($j = $rows; $j >= 1; $j--) — fixed width; spaces while $j > $i, else print $k.
printf("%3d", $k++) — fixed-width columns; counter continues across rows.
echo PHP_EOL ends the row after the inner loop finishes.
Total numbers = n(n+1)/2 — O(n²) time, O(1) extra memory.
$rows = 5Trace each outer-loop value of $i, leading spaces, numbers printed from $k, and full row output.
$i | Leading space groups | Numbers printed | Row output |
|---|---|---|---|
1 | 4 | 1 | 1 |
2 | 3 | 2, 3 | 2 3 |
3 | 2 | 4, 5, 6 | 4 5 6 |
4 | 1 | 7, 8, 9, 10 | 7 8 9 10 |
5 | 0 | 11, 12, 13, 14, 15 | 11 12 13 14 15 |
Leading space groups per row = $rows - $i — zero when $i = $rows. Total numbers printed = $rows($rows+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: flip $j > $i to $j <= $i for spaces and watch alignment break.
Foundation for right-aligned variants and continuous counter patterns.
Example: compare with Program 30 (descending digits) and Program 36 (decreasing sequence).
Practice %3d formatting and fixed-width columns.
Example: remove %3d and watch two-digit values misalign.
Add three-space groups for alignment once the two-loop structure works.
Example: use a single space instead of " " and watch columns drift.
Triangular totals make O(n²) concrete for beginners.
Example: count printed numbers for $rows = 5 — total is 1+2+3+4+5 = 15.
Pair the pattern with is_numeric($input) checks and positive-row validation.
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 broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace $i, $j, and $k on paper for $rows = 3 before coding — watch how leading spaces shrink each row.
Small habits that keep number-pattern code clean.
Counter $k must start at 1 before the outer loop and persist across rows.
is_numeric($input)Avoid undefined behavior when the user types letters instead of a number.
Only call echo PHP_EOL after the inner loop finishes the row.
Write the next value of $k for each ($i, $j) pair before coding the loops.
Trace $i = 1..3 on paper before coding the full $rows = 5 demo.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put echo PHP_EOL inside the inner loop.
Mistakes that commonly break right-aligned incremental triangles.
Each digit lands on its own line — you get a column, not a triangle.
→ Use printf("%3d", $k++); echo PHP_EOL only after the inner loop.
Putting $k = 1 inside the outer loop restarts the sequence on every row.
→ Declare $k = 1 once before the outer loop; only increment with $k++ when printing.
Single spaces instead of " " break column alignment with %3d.
→ Print three spaces while $j > $i to match the fixed-width number columns.
Plain echo $k++ makes two-digit values crowd earlier columns.
→ Use printf("%3d", $k++) for consistent column width.
Letters or empty input leave $rows uninitialized or unchanged.
→ Call is_numeric($input) and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 with leading spaces — one value, one row.
Outer loop never runs when $rows < 1 — print nothing or show a message.
$rows < 1Treat as invalid; re-prompt instead of silent empty output.
Two rows: 1 and 2 3.
Unchecked CLI input leaves $rows unset — call is_numeric($input) first.
Total numbers = $rows($rows+1)/2 — grows quadratically with rows.
Try these variations to lock in the pattern.
$j > $i spacing, different fill$i, $k = $i($i+1)/2 + 1$i more numbersis_numeric($input) until $rows >= 1$k = 1 before loops. Inner loop runs $j = $rows..1 — print spaces while $j > $i, else %3d with $k++.printf("%3d", $k++) stays on the line; echo PHP_EOL advances — mix them carefully.$rows >= 1 for interactive programs; $rows = 1 prints a single 1.$rows - $i — compare with Program 30 where digits descend instead of a counter.Quick Takeaway: outer $i = 1..$rows, inner $j = $rows..1, spaces while $j > $i, else %3d with $k++, then echo PHP_EOL.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The right-aligned incremental number triangle is a compact lesson in nested loops, continuous counters, and formatted output: print spaces while $j > $i, use printf("%3d", $k++) for each number, and end each row with echo PHP_EOL. Master the fixed-$rows version, then try user input and a smaller trace demo.
Practice the three examples above, then continue to Program 36 for the decreasing right-aligned sequence.
Keep $k outside the outer loop — validate $rows when reading from the console.
for ($i = 1; $i <= $rows; $i++) in the outer loopfor ($j = $rows; $j >= 1; $j--) with fixed widthprintf("%3d", $k++)is_numeric($input) instead of ignoring bad inputecho PHP_EOL inside the inner loop$k = 1 inside the outer loop" " for alignment$rows = 1 edge casePrint the pattern the beginner-friendly way.
$k++ each print
DefinitionLeading spaces
CodeFixed width
Codeecho PHP_EOL after $j loop
ShapeO(n²) time
AnalysisA counter $k starts at 1 and increments every time a number is printed. Leading spaces appear while $j > $i, and printf("%3d", $k++) keeps columns aligned as values grow past single digits.
Move on to the right-aligned decreasing number sequence in the PHP number-pattern series.
12 people found this page helpful