Mirror Digits Around 0 in C++

What You’ll Learn
How to print a mirror digit pattern centered around 0 in C++. Each row prints an increasing digit run, then 0, then a decreasing digit run.
This pattern is excellent practice for controlling loop ranges and building symmetry in console output.
⭐ Pattern Output
For the default setup (0 centered with digits up to 9), the pattern looks like this:
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321Complete C++ Program
This version matches the classic output by building rows from the edges inward and placing 0 at the center.
#include <iostream>
using namespace std;
int main() {
int i, j, k;
for (i = 10; i >= 1; i--) {
for (j = i; j < 10; j++) {
cout << j;
}
cout << "0";
for (k = 9; k >= i; k--) {
cout << k;
}
cout << "\n";
}
return 0;
}🧠 How It Works
Outer loop controls the starting digit
for (i = 10; i >= 1; i--) makes each next row start one digit earlier, growing the row.
Print the left side (ascending)
for (j = i; j < 10; j++) prints digits from i up to 9 (nothing prints when i = 10).
Print the center 0
cout << "0"; ensures every row has a center digit.
Print the right side (descending)
for (k = 9; k >= i; k--) prints digits from 9 down to i, mirroring the left side.
Mirrored digits around 0
Each row has symmetric digits on both sides of 0. For n rows, runtime is O(n²).
Variation — User Input Version
Let the user pick the maximum digit (recommended 1 to 9) using cin:
#include <iostream>
using namespace std;
int main() {
int maxDigit;
int i, j, k;
cout << "Enter max digit (1..9): ";
cin >> maxDigit;
for (i = maxDigit + 1; i >= 1; i--) {
for (j = i; j <= maxDigit; j++) {
cout << j;
}
cout << "0";
for (k = maxDigit; k >= i; k--) {
cout << k;
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Validate input (e.g., ensure
1 <= maxDigit <= 9) - Add spaces between digits to keep alignment when experimenting with larger values
- Replace the center
0with another digit or symbol - Right-align the output by adding leading spaces per row
- Turn it into a diamond by printing the bottom half as well
Avoid
- Allowing
maxDigit> 9 if you expect single-digit formatting - Forgetting the newline after each row
- Mixing up the loop bounds (the mirror depends on matching ranges)
- Using
endlinside tight loops unnecessarily
Key Takeaways
Each row is left half + 0 + right half.
The left side prints i..9, and the right side prints 9..i.
Starting from i = 10 creates the first line as just 0.
This pattern is a clean example of building symmetry using loop ranges.
❓ Frequently Asked Questions
i = 10, so the loop for (j = i; j < 10; j++) prints nothing and the loop for (k = 9; k >= i; k--) also prints nothing. Only the center 0 is printed.cout << "0"; with another character or digit (for example cout << "*";).cout << j << ' '; and cout << k << ' ';. This makes the symmetry easier to see.Explore More C++ Number Patterns!
Mirror patterns like this one are a fun way to practice nested loops and symmetry.
This pattern is essentially a symmetric sequence builder: print an ascending range, then a center token, then the descending range. The same technique works for letters, symbols, and even custom sequences.
12 people found this page helpful
