A rotating number pattern prints a fixed-width row that starts at i, goes up to rows, then wraps with i-1 down to 1. Every row has exactly rows digits.
Remember
Rule: for i from 1 to rows
print j from i up to rows
print (k-1) while k from i down to 2
12345
23451
34521
45321
54321 ← rows = 5
Two inner loops per row build the rotation. Follows the continuous triangle in Program 38; next is Program 40.
Approach
How to Solve It
Start with fixed rows = 5, then generalize with scanf — optionally demo a smaller count for tracing.
Method
Idea
Best for
Forward then wrap
Print i..rows, then i-1..1
Learning, interviews, exams
User input rows
Same loops; read rows with scanf
Reusable demos and labs
Pseudocode
Pseudocode
for i from 1 to rows:
for j from i to rows:
print j (no newline)
for k from i down to 2:
print (k - 1) (no newline)
print newline
Cheat sheet
Goal
Pattern
Walk each row
for (i = 1; i <= rows; i++)
Forward i..rows
for (j = i; j <= rows; j++) printf("%d", j);
Wrap i-1..1
for (k = i; k > 1; k--) printf("%d", k - 1);
End the row
printf("\n");
Digits per row
Always rows
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d", j) / printf("%d", k - 1)
Stays on the same line
Each digit in both segments
printf("\n")
Ends the current line
After both inner loops
Print all digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the rotating pattern updates instantly — capped at 9 so every value stays a single digit.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 25 digits
12345
23451
34521
45321
54321
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i and list both segments of the rotation.
i
Forward i..rows
Wrap i-1..1
Printed row
1
1, 2, 3, 4
(none)
1234
2
2, 3, 4
1
2341
3
3, 4
2, 1
3421
4
4
3, 2, 1
4321
Each of n rows prints n digits → n² digits total. That square count is why time is O(n²).
Code
C Programs
Three complete programs: fixed rows, scanf input, and a small demo. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded row count — forward segment then wrap segment on each row.
C
#include <stdio.h>
int main(void)
{
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;
}
Output
12345
23451
34521
45321
54321
How It Works
1. Outer loop grows i.i runs from 1 to 5 — each pass starts the row one digit later.
2. Forward segment.j runs from i to rows — the left part of the rotation.
3. Wrap + newline.k runs from i down to 2 printing k - 1; then printf("\n") ends the row.
When i = 1 you get 12345; when i = 3 you get 34521.
Example 2 — User Input Version
Read rows with scanf and reject invalid input. Same forward-and-wrap core.
C
#include <stdio.h>
int main(void)
{
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;
}
Output (when user enters 3)
Enter rows: 3
123
231
321
How It Works
1. Prompt and validate. Exit early if scanf fails or rows < 1.
2. Same core. Only the hard-coded 5 becomes the user value.
3. Safer input tip. Prefer an explicit message instead of a silent exit:
Safer input
if (scanf("%d", &rows) != 1 || rows < 1)
{
printf("Enter a positive whole number of rows.\n");
return 1;
}
Example 3 — Compact rows = 3
Same forward and wrap loops with a smaller row count for quick paper tracing.
C
#include <stdio.h>
int main(void)
{
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;
}
Output
123
231
321
How It Works
1. Same structure. Only rows changes from 5 to 3 — both inner loops stay identical.
2. Trace on paper. For i = 2: forward prints 2 3, wrap prints 1 → 231.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
one loop
Missing wrap
Skipping the second loop leaves unfinished rows like 345 instead of 34521. Keep both segments.
print k
Wrong wrap values
Printing k instead of k - 1 duplicates i and never reaches 1. Always print k - 1.
\n inside
Broken rows
If printf("\n") sits inside either inner loop, each digit lands on its own line. Call the newline only after both segments.
rows > 9
Multi-digit values
printf("%d", 10) prints two characters and the fixed-width look drifts. Clamp demos to 1–9.
rows = 1
Smallest rotation
Output is a single 1 — only the forward loop runs. A good sanity check.
scanf
Check the return value
If scanf fails, rows may be uninitialized — always test scanf(...) == 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(rows²)
O(1)
Small demo (Example 3)
O(rows²)
O(1)
Each of n rows prints n digits → n² total. For rows = 5 that is 25 printed digits.
Remember
Key Takeaways
Rule: print i..rows, then i-1..1 via k - 1.
Fixed width: every row prints exactly rows digits — forward count plus wrap count always sums to rows.
Break the row: call printf("\n") only after both segments.
Complexity:O(n²) time from n² digits; O(1) extra space.
One line: for each i, print i..rows then i-1..1, then printf("\n").
Frequently Asked Questions
For 5 rows: 12345, 23451, 34521, 45321, 54321 — each row starts at the row number and wraps back to 1.
The first loop prints i..rows (forward segment). The second loop prints i-1 down to 1 (wrap segment). Together they always produce rows digits.
After printing 2 3 4 5, the wrap loop prints k-1 while k runs from i down to 2 — for i=2 that prints 1.
Exactly rows digits every time — (rows - i + 1) forward plus (i - 1) wrap = rows.
Program 37 builds palindrome rows (i..2 then 1..i). Program 39 rotates: i..rows then i-1..1.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because each row prints n digits.
Check scanf's return value and reject non-positive row counts so the outer loop has a valid range.
🤔
Did you know?
Each row starts at i, prints i..rows, then wraps with i-1..1. Row i always prints exactly rows digits — total digits = n².