Split Mirror Number Pattern in C++

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

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:

Output
1        1
12      21
123    321
1234  4321
1234554321
1

Complete 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.

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

1

Fix the width

int n = 5; means each row prints 2n characters (numbers or spaces).

Setup
2

Left half: print 1..i

For columns j = 1..n, print j only when j <= i; otherwise print a space.

Left half
3

Right half: mirror back to 1

For k = n..1, print a space while k > i, then print numbers once k <= i.

Right half
4

The gap shrinks each row

As i increases, fewer spaces are printed in the middle until the last row becomes 1234554321.

Alignment
=

Split mirror pattern

Since each row prints \(2n\) characters for \(n\) rows, time complexity is O(n²).

2

Variation — User Input Version

Let the user decide the value of n using cin:

C++
#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 5 in multiple places (use n)
  • Using tabs instead of spaces (tabs can render inconsistently)
  • Forgetting the newline after each row
  • Using endl in tight loops unnecessarily

Key Takeaways

1

The left half prints numbers where j <= i; otherwise it prints spaces.

2

The right half mirrors by printing k when k <= i.

3

The middle gap shrinks as i increases until the halves meet.

4

This is a classic pattern for learning alignment and conditional printing.

❓ Frequently Asked Questions

On the last row, 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.
Yes. You can print fewer spaces in the middle by changing the width or by printing the left half with extra spacing and adjusting the right half accordingly.
One loop makes the left half easy (1..n). A second loop running in reverse (n..1) makes it straightforward to mirror the digits on the right side while still controlling spaces.
O(n²) because each of the n rows prints 2n characters.

Explore More C++ Number Patterns!

Once you’re comfortable with spaces and alignment, try hollow patterns and diagonals for an extra challenge.

All Number Patterns →
Did you know?

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.

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