Shape Rule
Fixed width, rotating start
Every row prints exactly $rows digits; the starting value shifts from 1 up to $rows.

The rotating number pattern shifts the starting digit each row while keeping fixed width. This tutorial covers the two-part row rule, dual inner loops, a live preview, algorithm steps, worked PHP examples, edge cases, and complexity.
Fixed width, rotating start
Every row prints exactly $rows digits; the starting value shifts from 1 up to $rows.
i..rows
for ($j = $i; $j <= $rows; $j++) prints the ascending run like 2345.
Wrap-around tail
for ($k = $i; $k > 1; $k--) appends $k-1 to complete the row.
Row start i
for ($i = 1; $i <= $rows; $i++) shifts the rotation each line.
1–20 rows
Pick a row count and draw the rotating pattern instantly in the browser.
Complexity
Total digits = n × n; extra memory stays O(1).
A rotating number pattern keeps every row the same length while the sequence wraps around. With $rows = 5, the output is 12345, 23451, 34521, 45321, and 54321.
In PHP you solve it with one outer loop and two inner loops per row: print $i..$rows, append $i-1..1, then call echo PHP_EOL to move to the next line.
It teaches dual inner loops on the same row — a key step before wrap-around and cyclic patterns.
Part 1: $i..$rows; Part 2: $i-1..1.
Every row prints exactly $rows digits.
echo $j and echo $k - 1 in the inner loops; echo PHP_EOL after.
Follow Program 38 sequential triangle; continue to Program 40 alternating 1/0.
In short: for each row $i from 1 to $rows, print $i..$rows then $i-1..1, then call echo PHP_EOL.
Given a positive integer $rows, print a rotating number pattern: each row $i prints $i..$rows then $i-1..1 — exactly $rows digits per row.
// $rows = 5
//12345
//23451
//34521
//45321
//54321 | Item | Type | Description |
|---|---|---|
$rows | int | Number of rotating rows to print (typically ≥ 1). |
| Printed output | text | Fixed-width rows of rotating digits; row $i has exactly $rows digits. |
for i from 1 to rows:
for j from i to rows: print j
for k from i down to 2: print k-1
print newline | Approach | Idea | Best for |
|---|---|---|
| Dual inner loops per row | Outer row + two inner parts | Learning and interviews |
| Spaced / formatted output | Add spaces or printf("%2d") between digits | Readability for $rows > 9 |
| Goal | Pattern |
|---|---|
| Walk each row | for ($i = 1; $i <= $rows; $i++) |
| Print ascending run | for ($j = $i; $j <= $rows; $j++) echo $j; |
| Append wrap-around | for ($k = $i; $k > 1; $k--) echo $k - 1; |
| End the row | echo PHP_EOL; |
| Program 38 variant | Shrinking width with global k counter instead of rotation |
Same rotating row — the two inner loops and their roles.
$i..$rowsPrints 2345 when $i = 2 and $rows = 5
$i-1..1Appends 1 to complete 23451
always nBoth parts together always print $rows digits
trace i=2Dry-run one row before coding the full pattern
Reach for this pattern when teaching two inner loops on the same row.
Most PHP pattern series start here before pyramids and diamonds.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible row count.
Compare Program 38 sequential triangle and Program 40 alternating 1/0 next.
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 rotating number pattern in the browser.
Three complete PHP programs — fixed row count, fgets(STDIN) input, and a spaced-output variant. Click View Output to reveal sample console results.
Print five rows with dual inner loops per line.
$rows = 5Hard-coded size — two inner loops build each rotating row.
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j <= $rows; $j++) {
echo $j;
}
for ($k = $i; $k > 1; $k--) {
echo $k - 1;
}
echo PHP_EOL;
} When $i = 2, the first loop prints 2345 and the second appends 1, giving 23451. When $i = 5, the first loop prints 5 and the second appends 4321, giving 54321.
Let the user choose the height at runtime.
Read the maximum digit with trim(fgets(STDIN)) (check is_numeric() in real apps).
<?php
echo "Enter the maximum number: ";
$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 = $i; $j <= $rows; $j++) {
echo $j;
}
for ($k = $i; $k > 1; $k--) {
echo $k - 1;
}
echo PHP_EOL;
} Same dual-loop core as Example 1; only the source of $rows changes. Non-numeric input fails is_numeric() — validate before casting to int for safer labs.
Same rotation with spaces between digits for easier reading.
Append a space after each digit so multi-digit rows stay readable.
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j <= $rows; $j++) {
echo $j . " ";
}
for ($k = $i; $k > 1; $k--) {
echo ($k - 1) . " ";
}
echo PHP_EOL;
} Same loop structure; only the print calls add + " " after each digit. Essential when $rows exceeds 9 or when demonstrating output formatting.
Set $rows = 5; and use fgets(STDIN) when reading input. Set loop variables $i, $j, $k.
for ($i = 1; $i <= $rows; $i++) picks the starting digit for each rotating row.
for ($j = $i; $j <= $rows; $j++) prints the main run with echo $j.
for ($k = $i; $k > 1; $k--) appends $k-1, then echo PHP_EOL ends the row.
Total digit prints: n × n — O(n²) time, O(1) extra memory.
$rows = 4Trace each row: part 1 ($i..$rows) plus part 2 ($i-1..1).
$i | Part 1 | Part 2 | Full row |
|---|---|---|---|
1 | 1234 | | 1234 |
2 | 234 | 1 | 2341 |
3 | 34 | 21 | 3421 |
4 | 4 | 321 | 4321 |
Total digit prints: 4 × 4 = 16 = n².
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change $j <= $i and watch the shape change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: change $k start to 100 for a shifted sequence.
Practice echo vs echo PHP_EOL 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: use %4d when values exceed two digits.
Square totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 → 100.
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 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: learn the two-loop row version first; then try spaced output for larger rows.
Small habits that keep number-pattern code clean.
Use $rows (or $n) and keep $i/$j/$k for loops — or rename to $row/$col.
fgets(STDIN)Validate with is_numeric() when the user types letters instead of a number.
Only call echo PHP_EOL after the inner loop finishes the row.
Add spaces or %2d when rows exceeds 9.
Trace $rows = 3 on paper before coding larger demos.
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 rotating number patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use echo $j and echo $k - 1; echo PHP_EOL only after both inner loops.
Skipping the second inner loop leaves rows short — e.g. 2345 instead of 23451.
→ Run both loops: $j = $i..$rows then $k = $i..2 printing $k-1.
Omitting echo PHP_EOL glues every digit onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input throw undefined rows.
→ Prefer fgets(STDIN) and re-prompt on failure.
Switching to i = 0 without adjusting the inner bound prints an empty first row or wrong counts.
→ If 0-based, print i with wrong inner bound (e.g. j <= i + 1).
Check these inputs before calling the solution done.
Output is just 1 on one line.
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² characters — fine for labs, noisy for huge n.
Unchecked fgets(STDIN) leaves $rows unset — call is_numeric($input) first.
Try alphabet rotation (A..E) using the same two-loop structure.
Try these variations to lock in the pattern.
$k counter with shrinking rows$rows down to 1n² — every row has n digits.echo stays on the line; echo PHP_EOL advances — mix them carefully.$rows > 0 for interactive programs; $rows = 1 should print a single 1.Quick Takeaway: outer loop picks start $i, two inner loops build the row, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
| Spaced output (Example 3) | O(rows²) | O(1) |
The rotating number pattern combines one outer loop with two inner loops per row — a natural step after sequential triangles. Master the compact digit output first, then optionally add spaces for readability.
Practice the three examples above, then continue to Program 40 for the alternating 1/0 pattern.
Row $i prints $i..$rows then $i-1..1 — keep echo PHP_EOL only after both inner loops finish.
$i..$rows) and part 2 ($i-1..1) before codingecho $j and echo $k - 1 in the inner loops and echo PHP_EOL after each row$rows ≥ 1 for interactive programsis_numeric($input) before using $rowsecho PHP_EOL inside the inner digit loop$rows = 1 edge casePrint the pattern the beginner-friendly way.
Row $i: $i..$rows then $i-1..1
Controls each row
CodePart 1 + Part 2 per row
Logicn digits every row
I/OO(n²) time
AnalysisEach row prints $i..$rows then $i-1..1 — two inner loops that create the rotation. Every row has exactly $rows digits, so total output is n² for n rows.
Move on to the alternating 1/0 pattern in the PHP number-pattern series.
12 people found this page helpful