A rotating number pattern prints a fixed-width row of digits that starts at the row index, continues up to rows, then wraps back down to 1 — like a circular left shift of 1..rows.
Remember
Rule: row i prints i..rows, then (i-1)..1
12345
23451
34521
45321
54321 ← 5 rows (each row has 5 digits)
Unlike Program 37 (palindrome rows), this shape always uses digits from 1 to rows exactly once per row — only the starting point rotates.
Approach
How to Solve It
One outer loop and two inner loops: print the forward segment, then the wrap segment.
Method
Idea
Best for
Forward + wrap
Print i..rows, then i-1..1
Learning, interviews, exams
Count check
Forward length + wrap length always equals rows
Verifying your bounds on paper
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
Print digits without a newline, then end the row once. cout << endl also ends the line and flushes; "\n" is enough for these demos.
Try it
Live Preview
Change the row count and the rotating pattern updates instantly — each row keeps a fixed width of rows digits.
Whole numbers from 1 to 9 (single digits). 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, the forward segment, the wrap segment, and the full row.
i
Forward
Wrap
Printed row
Digits
1
1..4
(none)
1234
4
2
2..4
1
2341
4
3
3..4
2 1
3421
4
4
4
3 2 1
4321
4
Total digits: 4 × 4 = 16 = n². Row 1 has no wrap; the last row is the full reverse n..1.
Code
C++ Programs
Three complete programs: fixed rows, cin input, and a small trace demo. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded row count — ideal for first demos and screenshots.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 5;
int i, j, k;
for (i = 1; i <= rows; ++i)
{
for (j = i; j <= rows; ++j)
cout << j;
for (k = i; k > 1; --k)
cout << k - 1;
cout << "\n";
}
return 0;
}
Output
12345
23451
34521
45321
54321
How It Works
1. Outer loop picks the start digit.i runs from 1 to rows — each row begins at i.
2. Forward segment. Print i through rows. For i = 3 that is 345.
3. Wrap segment. While k runs from i down to 2, print k - 1 — for i = 3 that adds 21, giving 34521.
4. End the row.cout << "\n" only after both inner loops finish.
Example 2 — User Input Version
Read the row count at runtime. Prefer validating cin (shown in the tip below).
C++
#include <iostream>
using namespace std;
int main()
{
int rows;
int i, j, k;
cout << "Enter rows: ";
cin >> rows;
for (i = 1; i <= rows; ++i)
{
for (j = i; j <= rows; ++j)
cout << j;
for (k = i; k > 1; --k)
cout << k - 1;
cout << "\n";
}
return 0;
}
Output (when user enters 3)
Enter rows: 3
123
231
321
How It Works
1. Prompt and read. Ask for a row count, then store it in rows.
2. Same rotating core. Only the source of rows changes — the print logic matches Example 1.
3. Safer input tip. Prefer:
Safer input
if (!(cin >> rows) || rows < 1)
{
cout << "Enter a whole number of rows (1 or more).\n";
return 1;
}
Example 3 — Compact rows = 3
Same forward and wrap loops with a smaller row count for quick tracing on paper.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 3;
int i, j, k;
for (i = 1; i <= rows; ++i)
{
for (j = i; j <= rows; ++j)
cout << j;
for (k = i; k > 1; --k)
cout << k - 1;
cout << "\n";
}
return 0;
}
Output
123
231
321
How It Works
1. Only rows changes. The two inner loops stay identical to Example 1.
2. Trace on paper.i = 1 → 123; i = 2 → 231; i = 3 → 321.
3. Fixed width. Every row still has exactly rows digits before you scale up.
Edge Cases & Pitfalls
Check these before calling the solution done.
k >= 1
Extra zero
If the wrap loop runs while k >= 1 and prints k - 1, you get a trailing 0. Stop at k > 1.
print k
Wrong wrap values
Print k - 1, not k. Printing k would duplicate the start digit (e.g. 23452 instead of 23451).
\n early
Broken rows
If cout << "\n" sits inside either inner loop, digits split across lines. End the row only after both loops finish.
j = 1
No rotation
Starting the forward loop at 1 every time reprints 12345 on every row. Start at i.
rows = 1
Single digit
Output is just 1 — only the forward loop runs. A good sanity check.
Bad cin
Validate rows
Check cin >> rows and require rows >= 1 before the outer loop.
Analysis
Time and Space Complexity
Program
Time
Extra space
Forward + wrap (Examples 1–3)
O(rows²)
O(1)
Each of n rows prints exactly n digits, so total work is n². Only a few integers of extra memory.
Remember
Key Takeaways
Rule: row i prints i..rows, then i-1..1 — always rows digits.
Wrap carefully: loop k > 1 and print k - 1 to avoid a trailing zero or a duplicate start.
Break the row: call cout << "\n" only after both inner loops.
Complexity:O(n²) time from n digits per row; O(1) extra space.
One line: for i = 1..rows, print i..rows then i-1..1, then end the line.
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. Program 39 rotates: i..rows then i-1..1.
Printing a digit stays on the same line. Printing a newline ends the current line. Digits use cout without a newline; the row break uses cout << "\n" after both inner loops.
O(n²) for n rows because each row prints n digits.
After cin >> rows, check failure and require rows ≥ 1 before the outer loop.
🤔
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².