Rotating Mirror Number Pattern in C++

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a pattern where each row starts at i, counts up to rows, and then appends the missing digits by counting down to 1.

This produces:

12345, 23451, 34521, 45321, 54321.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
12345
23451
34521
45321
54321
1

Complete C++ Program

Two loops per row: first prints the increasing tail (i..rows), second prints the decreasing head (i-1..1).

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

Set the size

rows = 5 is the maximum digit printed in each row.

Setup
2

Outer loop chooses the row start

i runs from 1 to rows. Each row begins from the current i.

Row control
3

Print i..rows

The first inner loop prints i, i+1, ..., rows. Example: when i=3, it prints 345.

Increasing part
4

Append (i-1)..1

The second inner loop appends i-1, i-2, ..., 1. For i=3, it appends 21, giving 34521.

Decreasing part
=

Rotating mirror rows

Each row prints rows digits, so the total work is O(n²) for n rows.

2

Variation — User Input Version

Let the user choose rows using cin:

C++
#include <iostream>
using namespace std;

int main() {
    int rows;
    int i, j, k;

    cout << "Enter the value of 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;
}

💡 Tips for Enhancement

Try These

  • Add spaces between digits for readability
  • Use a different range (e.g., 1..9) by changing rows
  • Left-pad each row to create a centered look
  • Print the same logic with characters (A..E) instead of numbers
  • Store each row in a string to simplify separators

Avoid

  • Hard-coding 5 everywhere instead of using rows
  • Printing 0 or negative rows without validation
  • Forgetting the newline after each row
  • Mixing loop roles (keep one loop for increasing and one for decreasing)

Key Takeaways

1

The first inner loop prints i..rows.

2

The second inner loop prints i-1..1 to fill the missing part.

3

Together they create rotating rows like 23451 and 34521.

4

Overall work is O(n²) for n rows.

❓ Frequently Asked Questions

Because the first loop prints rows-i+1 digits and the second prints i-1 digits. Their sum is always rows.
Yes. Print a space after each digit (or use a separator rule) in both loops. That makes the output easier to read.
When i = rows, the first loop prints only rows (5). The second loop appends 4,3,2,1, producing 54321.
O(n²) for n rows: there are n rows and each row prints n digits.

Explore More C++ Number Patterns!

Once you understand loop bounds, you can create many rotating and mirrored number designs.

All Number Patterns →
Did you know?

You can view this as a simple “rotation”: print from i to rows, then wrap around and print from 1 upward (here, achieved by printing i-1 down to 1).

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