C++ Mirror Number Pattern (0-Centered)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A 0-centered descending mirror prints ascending digits i..max, a fixed 0, then descending max..i — each row mirrors around zero.

Remember
Rule: for i from max+1 down to 1
        print j from i to max
        print 0
        print k from max down to i

0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321     ← max = 9

Follows the palindrome triangle in Program 27; next is the spaced mirror in Program 29.

How to Solve It

Outer loop decreases i from max+1 to 1. Print left half, then 0, then the mirror half.

MethodIdeaBest for
Three loops + fixed 0Ascend i..max, print 0, descend max..iLearning, interviews, exams
Spaced digitsSame loops with cout << j << " "Easier reading for larger max

Pseudocode

Pseudocode
for i from max + 1 down to 1:
    for j from i to max:
        print j
    print 0
    for k from max down to i:
        print k
    print newline

Cheat sheet

GoalPattern
Shrink start digitfor (i = max + 1; i >= 1; i--)
Left ascendingfor (j = i; j <= max; j++) cout << j;
Fixed centercout << "0";
Right mirrorfor (k = max; k >= i; k--) cout << k;
End of rowcout << "\n";

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << j / k / "0"Stays on the same lineEach digit on the row
cout << "\n"Ends the current lineAfter both half-loops and the center finish

Print digits without a newline, then end the row once.

Live Preview

Change the max digit and the 0-centered mirror updates instantly — capped at 9 for readable demos.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result max = 5 · 36 digits
0
505
45054
3450543
234505432
12345054321

Worked Walkthrough — max = 4

Trace how left half, center 0, and right half build each row as i decreases.

iLeftCenterRightPrints
5(none)0(none)0
4404404
33404334043
223404322340432
1123404321123404321

There are max + 1 rows. Digit total = (max + 1)² → O(n²) for max digit n.

C++ Programs

Three complete programs: fixed max = 9, cin input, and a compact max = 3 demo. Use View Output to reveal sample results.

Example 1 — Fixed max = 9

Hard-coded max digit — ascend i..9, print 0, then mirror 9..i.

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 shrinks the start. i runs from 10 down to 1 so each row grows.

2. Left, center, right. Print i..9, then 0, then 9..i on the same line.

3. First row is just 0. When i = 10, both half-loops are empty — only the center prints.

Example 2 — User Input Max

Read max with cin, validate, then use the same three-part core.

C++
#include <iostream>
using namespace std;

int main()
{
    int max;
    int i, j, k;

    cout << "Enter max digit (1-9): ";
    if (!(cin >> max) || max < 1 || max > 9)
    {
        cout << "Please enter a whole number from 1 to 9.\n";
        return 1;
    }

    for (i = max + 1; i >= 1; i--)
    {
        for (j = i; j <= max; j++)
            cout << j;

        cout << "0";

        for (k = max; k >= i; k--)
            cout << k;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Prompt and validate. Reject failed reads and values outside 1..9 before printing.

2. Same core. Replace hard-coded 9 / 10 with max / max + 1.

3. Safer input tip. Keep demos readable by capping at single digits:

Safer input
if (!(cin >> max) || max < 1 || max > 9)
{
    cout << "Enter a whole number from 1 to 9.\n";
    return 1;
}

Example 3 — Compact max = 3

Same structure with a small max — easy to confirm the empty first row and growing halves.

C++
#include <iostream>
using namespace std;

int main()
{
    int max = 3;
    int i, j, k;

    for (i = max + 1; i >= 1; i--)
    {
        for (j = i; j <= max; j++)
            cout << j;

        cout << "0";

        for (k = max; k >= i; k--)
            cout << k;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Four rows. i = 4 → 0; i = 3 → 303; then 23032 and 1230321.

2. Trace on paper. If you forget cout << "0", the halves glue together with no center.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for max 9 or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

no 0

Missing center

Without cout << "0" the halves join with no pivot — keep the center print between both loops.

i = max

Skipped first row

Starting at i = max skips the lone 0 row. Outer loop must begin at max + 1.

\n inside

Broken rows

If cout << "\n" sits inside either half-loop, the mirror splits across lines. Call it only after the full row.

max = 1

Smallest mirror

Output is 0 then 101 — a good sanity check for bounds and the center print.

max > 9

Multi-digit glue

Values above 9 print multi-digit numbers that run together. Cap demos or add spaces.

cin

Check the stream

Validate cin >> max before looping — a failed read leaves max unset or stale.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
Compact max = 3 (Example 3)O(n²)O(1)

There are n + 1 rows for max digit n. Digit total = (n + 1)² → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Three parts: print i..max, then 0, then max..i.
  • Start at max+1: the first row is only 0; then both halves grow.
  • Break the row: digits and center with no newline; cout << "\n" after the full row.
  • Complexity: O(n²) time from (n+1)² digits; O(1) extra space.

One line: for each i from max+1 down to 1, print i..max, then 0, then max..i, then newline.

Frequently Asked Questions

A 0-centered descending mirror: for max=9 you get 0 / 909 / 89098 / … / 1234567890987654321 — each row mirrors around a fixed zero.
cout << "0" sits between the ascending and descending loops, creating a fixed center on every row.
When i = max+1, both side loops are empty — only 0 is printed.
Starting at max+1 gives the single-0 row first; then i decreases and both halves grow toward max.
Program 27 mirrors 1..i on each row. Program 28 uses a fixed 0 center and grows digits toward max on both sides as i decreases.
Use cout << j << " " and cout << k << " " in the loops instead of cout << j.
After prompting, use if (!(cin >> max) || max < 1 || max > 9) to reject bad input before printing.
O(n²) for max digit n because there are n+1 rows and the digit total is (n+1)².

Did you know?

This pattern prints ascending digits from i to max, a fixed 0 in the center, then descending digits from max down to i. As i decreases, each row grows into the long mirror 1234567890987654321.

Next: Spaced Mirror Number Pattern

Continue with a mirror pattern that separates digits with spaces.

Program 29 tutorial →

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