Number + Star Mirror Pattern in C++

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

What You’ll Learn

How to print a mirrored number pattern where the middle expands with stars:

1234554321, 1234**4321, 123****321, 12******21, 1********1

We use three inner loops per row: increasing numbers, star pairs, and decreasing numbers.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1234554321
1234**4321
123****321
12******21
1********1
1

Complete C++ Program

Each row prints 1..i, then ** repeated, then i..1. As i decreases, the star block grows.

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

int main() {
    int rows = 5;
    int i, j, k, m;

    for (i = rows; i >= 1; i--) {
        for (j = 1; j <= i; j++) cout << j;
        for (k = i; k < rows; k++) cout << "**";
        for (m = i; m >= 1; m--) cout << m;
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Outer loop (rows shrinking)

for (i = rows; i >= 1; i--) reduces the number range each row.

Row control
2

Left numbers (1 to i)

for (j = 1; j <= i; j++) prints 123...i.

Left side
3

Middle stars

for (k = i; k < rows; k++) runs rows - i times and prints "**" each time.

Center fill
4

Right numbers (i to 1)

for (m = i; m >= 1; m--) prints the mirror half.

Right side
=

Mirrored pattern with expanding center

As i decreases, the left and right number ranges shrink and the star center expands.

2

Variation — User Input Version

Let the user choose the number of rows. The star center will expand accordingly.

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

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

    cout << "Enter the number of rows: ";
    cin >> rows;

    if (!cin || rows <= 0) return 0;

    for (i = rows; i >= 1; i--) {
        for (j = 1; j <= i; j++) cout << j;
        for (k = i; k < rows; k++) cout << "**";
        for (m = i; m >= 1; m--) cout << m;
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Print single stars by outputting * twice per iteration (or change the loop to print one * at a time)
  • Use spaces instead of stars to create a hollow mirrored number diamond effect
  • Store each row in a string to simplify formatting and alignment
  • Right-align rows by adding leading spaces before the left numbers
  • Experiment with different center characters (e.g., #, @)

Avoid

  • Using endl in tight loops (unnecessary flushing)
  • Forgetting that printing "**" doubles the star width
  • Mixing loop bounds (star loop should depend on rows and i)
  • Not validating user input when using cin

Key Takeaways

1

Each row has three parts: 1..i, a star center, and i..1.

2

The star center prints rows - i times (as pairs **).

3

As rows shrink, the number range contracts and the center expands.

4

This is a great exercise for coordinating multiple inner loops.

❓ Frequently Asked Questions

The star loop runs rows - i times. Each iteration prints "**", so the visible star count is 2(rows - i).
When i = rows, the condition k < rows is false immediately, so the star loop runs 0 times.
Yes. Replace cout << "**"; with cout << "*"; and adjust the loop bounds if you want the same total width.
O(n²) for \(n\) rows, because each line prints \(O(n)\) characters and there are \(n\) lines.

Explore More C++ Number Patterns!

Mixing symbols with mirrored numbers is a fun way to practice multi-loop pattern design.

All Number Patterns →
Did you know?

Mirror patterns are built by printing the left half and then printing a reversed right half—a technique that also appears in palindrome checks and string reversal problems.

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