Split Mirror Number Pattern in C++

What You’ll Learn
How to print a split mirror number pattern in C++: numbers grow from the left (1..i) and are mirrored on the right (i..1), with spaces forming a shrinking gap.
This pattern is a good exercise for mixing numbers and spaces inside loops to control alignment.
⭐ Pattern Output
For n = 5, the pattern looks like this:
1 1
12 21
123 321
1234 4321
1234554321Complete C++ Program
Two halves are printed with fixed width n: the first half prints 1..i, the second half prints i..1, and spaces fill the remaining positions.
#include <iostream>
using namespace std;
int main() {
int n = 5;
int i, j, k;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (j <= i)
cout << j;
else
cout << " ";
}
for (k = n; k >= 1; k--) {
if (k > i)
cout << " ";
else
cout << k;
}
cout << "\n";
}
return 0;
}🧠 How It Works
Fix the width
int n = 5; means each row prints 2n characters (numbers or spaces).
Left half: print 1..i
For columns j = 1..n, print j only when j <= i; otherwise print a space.
Right half: mirror back to 1
For k = n..1, print a space while k > i, then print numbers once k <= i.
The gap shrinks each row
As i increases, fewer spaces are printed in the middle until the last row becomes 1234554321.
Split mirror pattern
Since each row prints \(2n\) characters for \(n\) rows, time complexity is O(n²).
Variation — User Input Version
Let the user decide the value of n using cin:
#include <iostream>
using namespace std;
int main() {
int n;
int i, j, k;
cout << "Enter n: ";
cin >> n;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (j <= i)
cout << j;
else
cout << " ";
}
for (k = n; k >= 1; k--) {
if (k > i)
cout << " ";
else
cout << k;
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Validate input (e.g., ensure
n > 0) - Add a space between printed numbers for a clearer symmetric look
- Right-align the left half by printing leading spaces before it
- Replace digits with characters to create alphabet mirror patterns
- Build the full diamond by printing the pattern in reverse (n-1..1)
Avoid
- Hard-coding
5in multiple places (usen) - Using tabs instead of spaces (tabs can render inconsistently)
- Forgetting the newline after each row
- Using
endlin tight loops unnecessarily
Key Takeaways
The left half prints numbers where j <= i; otherwise it prints spaces.
The right half mirrors by printing k when k <= i.
The middle gap shrinks as i increases until the halves meet.
This is a classic pattern for learning alignment and conditional printing.
❓ Frequently Asked Questions
i == n, so both halves print all their digits and no spaces remain in the middle. That joins the two halves into one continuous mirrored sequence.Explore More C++ Number Patterns!
Once you’re comfortable with spaces and alignment, try hollow patterns and diagonals for an extra challenge.
Many console patterns are just conditional printing: decide whether to print a digit or a space at each column. Once you master that, you can build borders, diagonals, and hollow shapes.
12 people found this page helpful
