C++ Number Pattern (Rotating Digits)

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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.

How to Solve It

One outer loop and two inner loops: print the forward segment, then the wrap segment.

MethodIdeaBest for
Forward + wrapPrint i..rows, then i-1..1Learning, interviews, exams
Count checkForward length + wrap length always equals rowsVerifying 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

Cheat sheet

GoalPattern
Walk each rowfor (i = 1; i <= rows; ++i)
Forward segmentfor (j = i; j <= rows; ++j) cout << j;
Wrap segmentfor (k = i; k > 1; --k) cout << k - 1;
End the rowcout << "\n";
Digits per rowAlways rows — (rows - i + 1) + (i - 1)
Palindrome twinProgram 37 — up then mirror, not rotate

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << j / cout << k - 1Stays on the same lineEach digit
cout << "\n"Ends the current lineAfter both inner loops

Print digits without a newline, then end the row once. cout << endl also ends the line and flushes; "\n" is enough for these demos.

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 result 5 rows · 25 digits
12345
23451
34521
45321
54321

Worked Walkthrough — rows = 4

Trace each outer-loop value of i, the forward segment, the wrap segment, and the full row.

iForwardWrapPrinted rowDigits
11..4(none)12344
22..4123414
33..42 134214
443 2 143214

Total digits: 4 × 4 = 16 = n². Row 1 has no wrap; the last row is the full reverse n..1.

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;
}

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;
}

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;
}

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.

Time and Space Complexity

ProgramTimeExtra 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.

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².

Next: Alternating 1 and 0 Pattern

Continue with the next pattern in the C++ number-pattern series.

Program 40 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful