Number + Star Mirror Pattern in C++

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:
1234554321
1234**4321
123****321
12******21
1********1Complete C++ Program
Each row prints 1..i, then ** repeated, then i..1. As i decreases, the star block grows.
#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
Outer loop (rows shrinking)
for (i = rows; i >= 1; i--) reduces the number range each row.
Left numbers (1 to i)
for (j = 1; j <= i; j++) prints 123...i.
Middle stars
for (k = i; k < rows; k++) runs rows - i times and prints "**" each time.
Right numbers (i to 1)
for (m = i; m >= 1; m--) prints the mirror half.
Mirrored pattern with expanding center
As i decreases, the left and right number ranges shrink and the star center expands.
Variation — User Input Version
Let the user choose the number of rows. The star center will expand accordingly.
#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
endlin tight loops (unnecessary flushing) - Forgetting that printing
"**"doubles the star width - Mixing loop bounds (star loop should depend on
rowsandi) - Not validating user input when using
cin
Key Takeaways
Each row has three parts: 1..i, a star center, and i..1.
The star center prints rows - i times (as pairs **).
As rows shrink, the number range contracts and the center expands.
This is a great exercise for coordinating multiple inner loops.
❓ Frequently Asked Questions
rows - i times. Each iteration prints "**", so the visible star count is 2(rows - i).i = rows, the condition k < rows is false immediately, so the star loop runs 0 times.cout << "**"; with cout << "*"; and adjust the loop bounds if you want the same total width.Explore More C++ Number Patterns!
Mixing symbols with mirrored numbers is a fun way to practice multi-loop pattern design.
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.
12 people found this page helpful
