C++ Number Mirror Pattern (Asterisk Center)

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

What Is This Pattern?

A number & asterisk mirror prints ascending digits 1..i, a growing ** center, then descending digits i..1 on each row.

Remember
Rule: for i from n down to 1
        print 1..i
        print (n - i) pairs of **
        print i..1

1234554321
1234**4321
123****321
12******21
1********1     ← n = 5

Follows odd-length number rows in Program 22; next is the centered continuous pyramid in Program 24.

How to Solve It

Outer loop counts down from n. Three inner loops: ascending digits, star pairs, descending digits.

MethodIdeaBest for
Three inner loopsPrint 1..i, then ** pairs, then i..1Learning, interviews, exams
Custom fillReplace "**" with "##" or " "Same mirror shape, different center

Pseudocode

Pseudocode
for i from n down to 1:
    for j from 1 to i:
        print j
    for k from i to n - 1:
        print "**"
    for m from i down to 1:
        print m
    print newline

Cheat sheet

GoalPattern
Shrink digit rangefor (i = n; i >= 1; i--)
Left ascendingfor (j = 1; j <= i; j++) cout << j;
Center starsfor (k = i; k < n; k++) cout << "**";
Right descendingfor (m = i; m >= 1; m--) cout << m;
End of rowcout << "\n";

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << j / "**"Stays on the same lineDigits and star pairs
cout << "\n"Ends the current lineAfter all three inner loops finish

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

Live Preview

Change the size n and the mirror pattern 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 n = 5 · 5 rows
1234554321
1234**4321
123****321
12******21
1********1

Worked Walkthrough — n = 5

Trace how shrinking i adds more ** pairs while keeping the mirror.

iLeft** pairsRightPrints
5123450543211234554321
41234143211234**4321
31232321123****321
21232112******21
11411********1

Star pairs per row = n - i. Each row still has width proportional to n → O(n²) total work.

C++ Programs

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

Example 1 — Fixed n = 5

Hard-coded size — ascending digits, ** pairs, then descending digits.

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

int main()
{
    int n = 5;
    int i, j, k, m;

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

        for (k = i; k < n; k++)
            cout << "**";

        for (m = i; m >= 1; m--)
            cout << m;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer descends. i starts at n (full digits, no stars) and shrinks toward 1.

2. Three parts. Print 1..i, then n - i star pairs, then i..1.

3. Newline once. Call cout << "\n" only after all three inner loops finish.

Example 2 — User Input Size

Read n with cin, validate, then use the same three-loop core.

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

int main()
{
    int n;
    int i, j, k, m;

    cout << "Enter n: ";
    if (!(cin >> n) || n <= 0)
    {
        cout << "Please enter a positive integer.\n";
        return 1;
    }

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

        for (k = i; k < n; k++)
            cout << "**";

        for (m = i; m >= 1; m--)
            cout << m;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Prompt and validate. Reject failed reads and non-positive values before printing.

2. Same core. Ascending + stars + descending matches Example 1 — only n comes from the user.

3. Safer input tip. Cap demos for readable output:

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

Example 3 — Compact n = 3

Same structure with only three rows — easy to confirm left, center, and right.

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

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

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

        for (k = i; k < n; k++)
            cout << "**";

        for (m = i; m >= 1; m--)
            cout << m;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Three rows. i = 3 → 123321; i = 2 → 12**21; i = 1 → 1****1.

2. Trace on paper. If you skip the descending loop, the row is not mirrored — keep all three inners.

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

Edge Cases & Pitfalls

Check these before calling the solution done.

no mirror

Missing descending loop

Without for (m = i; m >= 1; m--) the row is not mirrored. Keep all three inner loops.

single *

Wrong center growth

Printing "*" instead of "**" grows the center too slowly and breaks symmetry with the digit halves.

\n inside

Broken rows

If cout << "\n" sits inside any inner loop, the mirror splits across lines. Call it only after all three finish.

n = 1

Single mirror

Output is just 11 — no stars. A good sanity check for input validation.

k bound

Star count off

Use for (k = i; k < n; k++) — exactly n - i pairs. A wrong bound misaligns the center.

cin

Check the stream

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

Time and Space Complexity

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

Each of n rows prints O(n) characters (digits + stars) → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Three parts: ascending 1..i, star pairs, descending i..1.
  • Grow the center: print "**" exactly n - i times as i shrinks.
  • Break the row: digits and stars in the inners; cout << "\n" after all three.
  • Complexity: O(n²) time from n rows of O(n) characters; O(1) extra space.

One line: for each i from n down to 1, print 1..i, then ** pairs, then i..1, then newline.

Frequently Asked Questions

A number and asterisk mirror: for n=5 you get 1234554321 / 1234**4321 / 123****321 / 12******21 / 1********1 — ascending digits, growing ** center, then descending digits.
Each iteration prints two asterisks so the center grows by 2 characters per row while keeping the pattern symmetric.
The pattern prints ascending numbers 1..i, then stars, then descending numbers i..1 on the same line.
Because i starts at n and decreases — each row prints fewer digits and more stars in the center.
cout << j stays on the same line. cout << "\n" ends the current line. Digits and stars use cout; the row break uses cout << "\n" after all three inner loops.
Three — ascending digits (j), star pairs (k), and descending digits (m).
Yes. Replace "**" with " " or "##" — digit loops stay the same.
O(n²) for size n because each of n rows prints O(n) characters overall.

Did you know?

Each row prints 1..i, then a growing block of ** pairs, then i..1 — three inner loops create a symmetric mirror. As i shrinks, the star block grows to keep row width consistent.

Next: Centered Continuous Pyramid

Continue with a centered pyramid that uses a running counter.

Program 24 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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