Shape Rule
$k / $m--
Odd rows print $k ascending; even rows print $m-- descending.

The alternating triangle prints 1, 3 2, 4 5 6, 10 9 8 7, 11 12 13 14 15 — numbers fill continuously but odd rows ascend and even rows descend. This tutorial covers running counter $k, row end $m, live preview, worked PHP examples, edge cases, and O(n²) complexity.
$k / $m--
Odd rows print $k ascending; even rows print $m-- descending.
$k marches on
$k never resets — it tracks the next number across all rows.
$m = $k + $i - 1
Compute $m before each inner loop for even-row descending output.
$i % 2
Use $i % 2 == 1 to pick ascending vs descending print direction.
3–12 rows
Pick a row count and draw the alternating number triangle instantly in the browser.
Complexity
Total values ≈ n(n+1)/2 — work grows as n².
An alternating ascending/descending number triangle fills numbers continuously from 1, but odd rows print ascending and even rows print descending. Row 2 shows 3 2; row 4 shows 10 9 8 7.
In PHP: outer for ($i = 1; $i <= $rows; $i++), set $m = $k + $i - 1, inner for ($j = 1; $j <= $i; $j++), if odd echo $k else echo $m--, increment $k, then echo PHP_EOL after the inner loop.
It is a running-counter exercise that alternates print direction on odd and even rows.
$i = 1..$rows picks each row number.
$k ascending, $m descending prints ascending or descending each row.
$k tracks the next number; odd rows print $k, even rows print from $m downward.
Follow Program 50 decreasing-increasing pattern; continue to Program 52 palindrome rows.
In short: outer $i=1..$rows, $m=$k+$i-1, inner $j=1..$i, odd echo $k else echo $m--, $k++, then echo PHP_EOL.
Given $rows = 5, print five lines: 1, 3 2, 4 5 6, 10 9 8 7, 11 12 13 14 15.
// $rows = 5 (conceptual output)
// 1
// 3 2
// 4 5 6
// 10 9 8 7
// 11 12 13 14 15 | Item | Type | Description |
|---|---|---|
$rows | int | How many lines to print (typically ≥ 1). |
i, j | int | Row index $i; running counter $k and row end $m = $k + $i - 1. |
| Printed output | text | $i values on row $i — growing triangle shape. |
k = 1
for i from 1 to rows:
$m = $k + $i - 1
for j from 1 to i:
if i odd: print k
else: print m; m = m - 1
k = k + 1
newline | Approach | Idea | Best for |
|---|---|---|
| Running counter $k | echo $k on odd rows, echo $m-- on even rows | Growing triangle — $i values per row |
| fgets(STDIN) input | (int) $input after is_numeric($input) | User-chosen row count |
| No trailing space | Print space only before 2nd+ values | Cleaner row formatting — Example 3 |
| Goal | Pattern |
|---|---|
| Set rows | $rows = 5; |
| Outer loop | for ($i = 1; $i <= $rows; $i++) |
| Init counter | $k = 1; |
| Row end | $m = $k + $i - 1; |
| Inner loop | for ($j = 1; $j <= $i; $j++) |
| Odd/even print | if ($i % 2 == 1) echo $k else echo $m--; then $k++ |
| Row break | echo PHP_EOL; after inner loop |
| Program 50 contrast | Decreasing-increasing pattern uses dual loops per row; this pattern uses running counter $k with odd/even direction |
How outer row selection, running counter $k, row end $m, and odd/even direction work together.
for ($i = 1; $i <= $rows; $i++)Picks row number $i — triangle height.
$m = $k + $i - 1Computed before the inner loop on each row.
if ($i % 2 == 1) $k
else $m--Odd rows ascending from $k; even rows descending from $m.
trace $i=4Dry-run row 4: $k=7, $m=10 → prints 10 9 8 7.
Reach for this pattern when teaching running counters, odd/even conditions, and alternating row direction.
Classic follow-up after decreasing-increasing patterns like Program 50.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible row count.
Compare with Program 50 (decreasing-increasing pattern), then continue to Program 52 (palindrome rows).
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in running counters, parity checks, and O(n²) thinking.
Choose a row count and draw the alternating number triangle pattern in the browser.
Three complete PHP programs — fixed $rows = 5, fgets(STDIN) input, and a no-trailing-space variant. Click View Output to reveal sample console results.
Print five rows with running counter $k — odd rows ascending, even rows descending.
$rows = 5Hard-coded size — compute $m = $k + $i - 1 and alternate print direction by row parity.
<?php
$rows = 5;
$k = 1;
for ($i = 1; $i <= $rows; $i++) {
$m = $k + $i - 1;
for ($j = 1; $j <= $i; $j++) {
if ($i % 2 == 1) {
echo $k . " ";
} else {
echo $m-- . " ";
}
$k++;
}
echo PHP_EOL;
} When $i = 4, $k = 7, $m = 10 — even row prints 10 9 8 7. Row 2 is even — values 2 and 3 print as 3 2.
Read $rows with fgets(STDIN) for flexible output size.
Same $k/$m counter logic; row count comes from user input.
<?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;
$k = 1;
for ($i = 1; $i <= $rows; $i++) {
$m = $k + $i - 1;
for ($j = 1; $j <= $i; $j++) {
if ($i % 2 == 1) echo $k . " ";
else echo $m-- . " ";
$k++;
}
echo PHP_EOL;
} Identical counter logic to Example 1; only the row count is dynamic.
Avoid trailing spaces on each row.
Print a space only before the second and later values on each row.
<?php
$rows = 5;
$k = 1;
for ($i = 1; $i <= $rows; $i++) {
$m = $k + $i - 1;
for ($j = 1; $j <= $i; $j++) {
if ($j > 1) echo " ";
if ($i % 2 == 1) echo $k;
else echo $m--;
$k++;
}
echo PHP_EOL;
} Same counter logic; only the output format avoids trailing spaces.
echo is built in; use fgets(STDIN) when reading input. Set $rows (e.g. 5).
for ($i = 1; $i <= $rows; $i++) — one iteration per output line.
Set $m = $k + $i - 1. If $i is odd, echo $k; if even, echo $m--. Increment $k each inner step.
echo PHP_EOL; after the inner loop moves to the next row.
Total prints ≈ n(n+1)/2 — O(n²) time, O(1) extra memory.
i = 4Trace row 4 with $rows = 5 to see how $k, $m, and even-row descending output build 10 9 8 7.
| Step | k | m | Printed |
|---|---|---|---|
| Before row 4 | 7 | — | — |
| Compute m | 7 | 10 | — |
| j=1 (even row) | 8 | 9 | 10 |
| j=2 | 9 | 8 | 9 |
| j=3 | 10 | 7 | 8 |
| j=4 | 11 | 6 | 7 |
Row output: 10 9 8 7. Then echo PHP_EOL moves to row 5 with k = 11.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: use fgets(STDIN) for dynamic row count — see Example 2.
Each row alternates print direction while $k marches forward continuously.
Example: compare row 4 (10 9 8 7) — even row prints from m downward.
Practice PHP_EOL vs print for multi-line vs single-line output.
Example: use echo $k . " ") in both loops for spaced output — Example 3.
Print a space only before the second and later values on each row.
Example: print $rows=5 without trailing spaces and compare formatting.
Growing inner bounds plus direction flip makes O(n²) concrete for beginners.
Example: count values for rows=5 → 1+2+3+4+5 = 15 printed numbers.
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 loop 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 odd/even direction shows immediately — even rows should print descending from $m.
Only loops and console output — no arrays or math libraries.
Change $rows, use fgets(STDIN), or remove trailing spaces with conditional echo.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the running counter $k first; then try fgets(STDIN) input and the spaced-digit variant in Example 3.
Small habits that keep number-pattern code clean.
Use $rows for height and $i/$j for row/column indices.
fgets(STDIN)Avoid crashes when the user types letters instead of a number.
Compute $m, run inner loop with direction check, increment $k, then echo PHP_EOL.
Use echo $k . " " or echo $m-- . " " inside the inner loop; one echo PHP_EOL per row after it finishes.
Trace $rows = 5, $i = 4 on paper — expect 10 9 8 7.
Pro Tip: if even rows look ascending, check whether you forgot $m = $k + $i - 1 or the odd/even condition.
Mistakes that commonly break alternating ascending/descending number triangles.
Each value lands on its own line — you get a column, not a triangle row.
→ Use echo $k or echo $m-- inside the inner loop; echo PHP_EOL only after it finishes.
Without computing $m before the inner loop, even rows cannot print descending correctly.
→ Always set $m = $k + $i - 1 before the inner loop on every row.
Omitting echo PHP_EOL after the inner loop glues all rows onto one line.
→ Always call echo PHP_EOL after the inner loop completes.
Resetting $k to 1 each row breaks the continuous number sequence.
→ Let $k continue across rows; only compute fresh $m each outer iteration.
Letters or empty input fail without validation.
→ Call is_numeric($input) before casting to (int).
Using literal 10 in loop bounds instead of variable $rows breaks dynamic input.
→ Use one $rows variable for the outer loop bound.
Check these inputs before calling the solution done.
Output is one line: 1.
Loop never runs — print nothing or show a message.
$rows < 0Treat as invalid; re-prompt instead of silent empty output.
Large row counts produce many values — fine for labs; use smaller n for quick demos.
Unchecked fgets(STDIN) input leaves $rows unset — call is_numeric($input) first.
Use conditional spacing to avoid trailing spaces — see Example 3.
Try these variations to lock in the pattern.
$rows = 3, 6, or 83 2 to 2 3for ($i = $rows; $i >= 1; $i--)n(n+1)/2 (e.g. 15 values for $rows=5).echo stays on the line; PHP_EOL advances — mix them carefully.$rows > 0 for interactive programs; $rows = 1 prints one value.echo $k; even rows use echo $m-- — always increment $k each inner step.Quick Takeaway: outer $i=1..$rows, $m=$k+$i-1, inner $j=1..$i, odd echo $k else echo $m--, $k++, then echo PHP_EOL.
| Program | Time | Extra space |
|---|---|---|
Fixed $rows = 5 (Example 1) | O(n²) | O(1) |
| fgets(STDIN) input (Example 2) | O(n²) | O(1) |
| No trailing space (Example 3) | O(n²) | O(1) |
The alternating triangle combines a running counter with odd/even row direction — a natural step after decreasing-increasing patterns. Master the fixed-$rows version first, then try fgets(STDIN) input and the no-trailing-space variant in Example 3.
Practice the three examples above, then continue to Program 52 for the increasing-decreasing palindrome pattern (1, 232, 34543…).
Keep echo PHP_EOL after the inner loop — one row break per outer iteration.
$i, running counter $k, row end $m = $k + $i - 1, and odd/even direction before codingecho $k . " " or echo $m-- . " ", then echo PHP_EOL after inner loop$rows ≥ 1 for interactive programsfgets(STDIN) return value before using $rows$k each row (breaks continuous sequence)$m = $k + $i - 1 before the inner loop$rows$rows = 1 edge casePrint the pattern the beginner-friendly way.
Each row prints $i values
$i = 1..$rows
CodeOdd: echo $k; even: echo $m--
Logicn(n+1)/2 prints
O(n²)
AnalysisNumbers fill continuously from 1, but each row alternates print direction — odd rows ascending (4 5 6), even rows descending (10 9 8 7). Compute row end with $m = $k + $i - 1.
Move on to the increasing-decreasing palindrome pattern in the PHP number-pattern series.
12 people found this page helpful