Shape Rule
Rotating row
Row i prints i..rows, then wraps with i-1..1 — exactly rows digits per row.

The rotating number pattern prints 12345, then 23451, then 34521, … — each row starts at i and wraps back to 1 — a natural follow-up after Program 38’s decreasing-width triangle. This tutorial covers forward and wrap-around inner loops, row rotation, nested loops, a live preview, worked C examples, edge cases, and complexity.
Rotating row
Row i prints i..rows, then wraps with i-1..1 — exactly rows digits per row.
i = 1..rows
for (i = 1; i <= rows; i++) — one rotating row per iteration.
i..rows
for (j = i; j <= rows; j++) — prints the increasing forward part of the row.
i-1..1
for (k = i; k > 1; k--) printf("%d", k - 1); — completes the row with wrap-around digits.
3–7 rows
Pick a row count and draw the rotating number pattern in the browser.
Complexity
Each row prints rows digits — total digits = n².
A rotating number pattern prints a circular-shift sequence on each row: 12345, then 23451, then 34521, and so on. With rows = 5, each row starts at the row number and wraps back to 1.
In C you use two inner loops per row: print printf("%d", j) from i up to rows, then print printf("%d", k - 1) from k = i down to 2, then printf("\n").
It combines forward and wrap-around inner loops to build rotation — a step after Program 38’s continuous decreasing triangle.
Forward segment.
Wrap segment.
Per row.
Follow Program 38; continue to Program 40 next.
In short: outer i = 1..rows, forward j = i..rows, wrap k = i..2 with k-1, then printf("\n").
Given rows = 5, print a rotating number pattern: for each row i, print ascending i..rows then wrap with i-1..1.
// rows = 5
//12345
//23451
//34521
//45321
//54321 | Item | Type | Description |
|---|---|---|
rows | int | Pattern height — number of rotating lines to print. |
i | int | Outer loop — current row (1 to rows). |
j | int | Forward loop — ascending from i to rows. |
k | int | Wrap loop — descending from i to 2, prints k-1. |
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 |
|---|---|---|
| Fixed rows | 12345, 23451, … | Learning and interviews |
| User-input rows | scanf("%d", &rows); | Configurable pattern size |
| Compact trace | rows = 3 on paper first | Debugging loop bounds |
| Goal | Pattern |
|---|---|
| Outer loop | for (i = 1; i <= rows; i++) |
| Forward segment | for (j = i; j <= rows; j++) printf("%d", j); |
| Wrap segment | for (k = i; k > 1; k--) printf("%d", k - 1); |
| End the row | printf("\n"); |
| User input | scanf("%d", &rows) |
Same rotating number pattern — different ways to control the row count.
i = 1..rowsOne rotating row per iteration
j = i..rowsAscending segment
k-1i-1 down to 1
rowsDigits per row
Reach for this pattern when teaching forward and wrap-around inner loops, circular rotation, and sequence design.
Natural follow-up — replaces decreasing-width rows with rotating sequences built from forward and wrap loops.
Practice forward then wrap loops to build circular-shift sequences on each row.
Combine loops with scanf and return-value checks for flexible row counts.
Compare Program 37 (palindrome) and Program 40 (next in series) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in dual inner loops, wrap-around logic, and O(n²) thinking.
Choose a row count between 3 and 7 and draw the rotating number pattern in the browser.
Three complete C programs — fixed rows, user input, and a smaller trace demo. Click View Output to reveal sample console results.
Print five rows of the rotating number pattern with forward and wrap-around inner loops.
rows = 5Hard-coded row count — ideal for first demos and screenshots.
#include <stdio.h>
int main() {
int rows = 5;
int i, j, k;
for (i = 1; i <= rows; ++i) {
for (j = i; j <= rows; ++j)
printf("%d", j);
for (k = i; k > 1; --k)
printf("%d", k - 1);
printf("\n");
}
return 0;
} When i = 3, the forward loop prints 3 4 5, the wrap loop prints 2 1 — output 34521. When i = 1, only the forward loop runs — output 12345.
Read the row count with scanf instead of hard-coding 5.
Read rows with scanf("%d", &rows) and validate the return value.
#include <stdio.h>
int main() {
int rows;
int i, j, k;
printf("Enter rows: ");
if (scanf("%d", &rows) != 1 || rows < 1) return 0;
for (i = 1; i <= rows; ++i) {
for (j = i; j <= rows; ++j)
printf("%d", j);
for (k = i; k > 1; --k)
printf("%d", k - 1);
printf("\n");
}
return 0;
} Same rotating 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 ignore scanf’s return value — always check it in safer labs.
Run with rows = 3 to trace every row on paper before scaling up.
rows = 3Same forward and wrap loops with a smaller row count for quick tracing.
#include <stdio.h>
int main() {
int rows = 3;
int i, j, k;
for (i = 1; i <= rows; ++i) {
for (j = i; j <= rows; ++j)
printf("%d", j);
for (k = i; k > 1; --k)
printf("%d", k - 1);
printf("\n");
}
return 0;
} Only rows changes from 5 to 3 — the two inner loops stay identical. Trace i = 1, 2, 3 on paper to see how each row rotates the sequence.
#include <stdio.h> brings in printf / scanf. Set loop variables i, j, k with rows = 5.
for (i = 1; i <= rows; i++) — ascending outer loop; one rotating row per iteration.
for (j = i; j <= rows; j++) — prints i, i+1, ..., rows.
for (k = i; k > 1; k--) — prints i-1, i-2, ..., 1 via k-1.
printf("\n") ends the row after both inner loops finish.
Each row prints exactly rows digits — total digits = n²; O(n²) time.
rows = 5Trace each outer-loop value of i, forward and wrap segments, and full row output.
i | Forward (i..rows) | Wrap (i-1..1) | Row output |
|---|---|---|---|
1 | 1, 2, 3, 4, 5 | — | 12345 |
2 | 2, 3, 4, 5 | 1 | 23451 |
3 | 3, 4, 5 | 2, 1 | 34521 |
4 | 4, 5 | 3, 2, 1 | 45321 |
5 | 5 | 4, 3, 2, 1 | 54321 |
Each row prints exactly rows digits — total digits = n × n = 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: swap forward and wrap loops and watch the rotation break.
Foundation for rotation-based patterns and circular-shift sequences.
Example: compare with Program 38 (decreasing) and Program 40 next.
Practice concatenated digit output without spaces between numbers.
Example: add j + " " between digits for a spaced rotation variant.
Swap digits for letters once the two-loop structure works.
Example: print (char)('A' + j - 1) for an A..E rotation pattern.
Square totals make O(n²) concrete for beginners.
Example: count digits for rows = 5 — total is 5×5 = 25 = 5².
Pair the pattern with scanf return 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 C 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, forward segment j = i..rows, and wrap segment on paper for rows = 3 before coding.
Small habits that keep number-pattern code clean.
Forward loop (j <= rows) must run before wrap loop (k > 1).
scanf return valueAvoid undefined behavior when the user types letters instead of a number.
Only call printf("\n") after both inner loops finish the row.
Write forward (i..rows) and wrap (i-1..1) for each row before coding.
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 printf("\n") inside an inner loop.
Mistakes that commonly break rotating number patterns.
Each digit lands on its own line — you get a column, not a pattern.
→ Use printf("%d", j) or printf("%d", k - 1); printf("\n") only after both inner loops.
Running wrap before forward breaks the rotation sequence.
→ Always print forward j = i..rows first, then wrap k = i..2 with k-1.
Using k >= 1 and printing k duplicates the start digit.
→ Keep for (k = i; k > 1; k--) printf("%d", k - 1); — stop at 2, print k-1.
Printing k in the wrap loop shifts the wrap segment by one.
→ Use printf("%d", k - 1) so wrap prints i-1..1.
Letters or empty input leave rows uninitialized or unchanged.
→ Check scanf return value and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 — only the forward loop runs.
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: 12 and 21.
scanf without a return check is unsafe — validate input.
Total digits = rows² — each row prints rows digits.
Try these variations to lock in the pattern.
i always prints rows digitsi..rows, wrap = i-1..1scanf return value until rows >= 1i = 1..rows. Forward j = i..rows, then wrap k = i..2 with k-1 — row i prints exactly rows digits.printf stays on the line; printf("\n") advances — mix them carefully.rows >= 1 for interactive programs; rows = 1 prints a single 1.i = 1, the wrap loop does not run — compare with Program 37 where each row is a palindrome.Quick Takeaway: outer i = 1..rows, forward j = i..rows, wrap k = i..2 with k-1, then printf("\n").
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The rotating number pattern is a compact lesson in wrap-around logic: print forward i..rows, then wrap with k-1 from k = i..2, and end each row with printf("\n"). Master the fixed-rows version, then try user input and a smaller trace demo.
Practice the three examples above, then continue to Program 40 for the next pattern in the series.
Forward loop must run before wrap loop — validate rows when reading from the console.
for (i = 1; i <= rows; i++) in the outer loopfor (j = i; j <= rows; j++) printf("%d", j);for (k = i; k > 1; k--) printf("%d", k - 1);scanf return value instead of ignoring bad inputprintf("\n") inside an inner loopk instead of k-1 in the wrap looprows = 1 edge casePrint the pattern the beginner-friendly way.
Forward segment
Definitioni-1..1
CodePer row
Codeprintf("\n") after both inner loops
ShapeO(n²) time
AnalysisEach row starts at i, prints i..rows, then wraps with i-1..1. Row i always prints exactly rows digits — total digits = n².
Move on to the next pattern in the C number-pattern series.
12 people found this page helpful