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 Java 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 Java you solve it with one outer loop and two inner loops per row: print i..rows, append i-1..1, then call System.out.println() 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.
System.out.print(j) in both inner loops; println() 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 System.out.println().
Given a positive integer rows, print consecutive integers starting at 1 in a triangle where row i contains exactly rows - i + 1 values.
// First 5 rows (conceptual shape)
// 1 2 3 4 5
// 6 7 8 9
// 10 11 12
// 13 14
// 15 | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines to print (typically ≥ 1). |
| Printed output | text | Left-aligned rows of consecutive integers; row i has rows - i + 1 values. |
for i from rows down to 1:
if i is even:
for j from i down to 1: print j
else:
for j from 1 to i: print j
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 %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++) System.out.print(j); |
| Append wrap-around | for (k = i; k > 1; k--) System.out.print(k - 1); |
| End the row | System.out.println(); |
| 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 Java pattern series start here before pyramids and diamonds.
Outer/inner bound practice with an immediate visual check.
Combine loops with Scanner 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 Java programs — fixed row count, Scanner 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.
public class RotatingNumberPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = i; j <= rows; j++) {
System.out.print(j);
}
for (int k = i; k > 1; k--) {
System.out.print(k - 1);
}
System.out.println();
}
}
} 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 Scanner.nextInt() (check hasNextInt() in real apps).
import java.util.Scanner;
public class RotatingNumberPatternInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the maximum number: ");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = i; j <= rows; j++) {
System.out.print(j);
}
for (int k = i; k > 1; k--) {
System.out.print(k - 1);
}
System.out.println();
}
sc.close();
}
} Same dual-loop core as Example 1; only the source of rows changes. Non-numeric input throws InputMismatchException with nextInt() — check hasNextInt() for safer labs.
Same rotation with spaces between digits for easier reading.
Append a space after each digit so multi-digit rows stay readable.
public class RotatingSpacedOutput {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = i; j <= rows; j++) {
System.out.print(j + " ");
}
for (int k = i; k > 1; k--) {
System.out.print((k - 1) + " ");
}
System.out.println();
}
}
} Same loop structure; only the print calls add + " " after each digit. Essential when rows exceeds 9 or when demonstrating output formatting.
System.out is built in; use Scanner when reading input. Set rows (fixed or from input).
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 System.out.print(j).
for (k = i; k > 1; k--) appends k-1, then println() 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 System.out.print vs row newline without complex math.
Example: put System.out.println() 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.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 still → 55.
Pair the pattern with Scanner 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 Java 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 for row/column — or rename to row/col.
ScannerAvoid crashes when the user types letters instead of a number.
Only call System.out.println() 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 System.out.println() 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 System.out.print(j) and System.out.print(k-1); println() only after both inner loops.
Using 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 System.out.println() glues every digit onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input throw undefined rows.
→ Prefer Scanner 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²/2 characters — fine for labs, noisy for huge n.
Unchecked Scanner leaves rows unset — call sc.hasNextInt() first.
Try alphabet rotation (A..E) using the same two-loop structure.
Try these variations to lock in the pattern.
k counter with shrinking rowsrows down to 1n² — every row has n digits.print stays on the line; println 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 println() only after both inner loops finish.
System.out.print in both inner loops and println() after each rowrows ≥ 1 for interactive programsScanner return value before using rowsSystem.out.println() inside the inner digit looprows = 1 edge casePrint the pattern the beginner-friendly way.
Row i: i..rows then i-1..1
DefinitionControls 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 Java number-pattern series.
12 people found this page helpful