Mirror Digits Around 0 in C++

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

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:

Output
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321
1

Complete C++ Program

This version matches the classic output by building rows from the edges inward and placing 0 at the center.

C++
#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

1

Outer loop controls the starting digit

for (i = 10; i >= 1; i--) makes each next row start one digit earlier, growing the row.

Row control
2

Print the left side (ascending)

for (j = i; j < 10; j++) prints digits from i up to 9 (nothing prints when i = 10).

Left half
3

Print the center 0

cout << "0"; ensures every row has a center digit.

Center
4

Print the right side (descending)

for (k = 9; k >= i; k--) prints digits from 9 down to i, mirroring the left side.

Right half
=

Mirrored digits around 0

Each row has symmetric digits on both sides of 0. For n rows, runtime is O(n²).

2

Variation — User Input Version

Let the user pick the maximum digit (recommended 1 to 9) using cin:

C++
#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 0 with 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 endl inside tight loops unnecessarily

Key Takeaways

1

Each row is left half + 0 + right half.

2

The left side prints i..9, and the right side prints 9..i.

3

Starting from i = 10 creates the first line as just 0.

4

This pattern is a clean example of building symmetry using loop ranges.

❓ Frequently Asked Questions

Because the first iteration uses 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.
Replace cout << "0"; with another character or digit (for example cout << "*";).
Yes. Print a space after each digit, e.g., cout << j << ' '; and cout << k << ' ';. This makes the symmetry easier to see.
O(n²) for n rows: each row prints O(n) digits and there are n rows.

Explore More C++ Number Patterns!

Mirror patterns like this one are a fun way to practice nested loops and symmetry.

All Number Patterns →
Did you know?

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.

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