C Number Mirror Pattern (Asterisk Center)

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

What Is This Pattern?

A number & asterisk mirror prints ascending digits, a growing ** center, then descending digits — so each row is a left–right mirror with a star fill in the middle.

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

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

Three inner loops drive the shape. Follows odd-length rows in Program 22; next is the centered pyramid in Program 24.

How to Solve It

Use a countdown outer loop and three inner loops — then optionally swap the center fill string.

MethodIdeaBest for
Three inner loops + **Ascend, star pairs, descendLearning, interviews, exams
Custom fillSame loops; print ## or spaces instead of **Variants once the core clicks

Pseudocode

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

Cheat sheet

GoalPattern
Shrink digit rangefor (i = n; i >= 1; i--)
Print 1..ifor (j = 1; j <= i; j++) printf("%d", j);
Print star pairsfor (k = i; k < n; k++) printf("**");
Print i..1for (m = i; m >= 1; m--) printf("%d", m);
End the rowprintf("\n");
Swap center fillprintf("##"); or printf(" ");

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d", j) / printf("**")Stays on the same lineDigits and star pairs
printf("\n")Ends the current lineAfter all three inner loops

Print the whole row without newlines, then end the row once.

Live Preview

Change the size and the mirror updates instantly — capped at 9 so every digit stays a single character.

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 · 50 chars
1234554321
1234**4321
123****321
12******21
1********1

Worked Walkthrough — n = 4

Trace each outer-loop value of i from 4 down to 1 and list the three parts of the row.

iLeft 1..iCenterRight i..1Printed row
41234(none)432112344321
3123**321123**321
212****2112****21
11******11******1

Every row is 2 × 4 = 8 characters wide. Total characters for size n: n × 2n = 2n² — why time is O(n²).

C Programs

Three complete programs: fixed size, scanf input, and a custom center fill. Use View Output to reveal sample results.

Example 1 — Fixed n = 5

Hard-coded size — countdown outer loop; three inner loops for left digits, **, and right digits.

C
#include <stdio.h>

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

    for (i = 5; i >= 1; i--)
    {
        for (j = 1; j <= i; j++)
            printf("%d", j);

        for (k = i; k < 5; k++)
            printf("**");

        for (m = i; m >= 1; m--)
            printf("%d", m);

        printf("\n");
    }

    return 0;
}

How It Works

1. Outer loop shrinks i. i runs from 5 down to 1 — fewer digits each row.

2. Left wing. j prints 1..i with printf("%d", j).

3. Center fill. k runs from i to n - 1, printing one ** pair per step — zero pairs on the first row.

4. Right wing + newline. m prints i..1; then printf("\n") ends the row.

When i = 5 you get 1234554321; when i = 3 you get 123****321.

Example 2 — User Input Version

Read n with scanf. Prefer checking its return value in real apps (shown in the tip below).

C
#include <stdio.h>

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

    printf("Enter n: ");
    scanf("%d", &n);

    for (i = n; i >= 1; i--)
    {
        for (j = 1; j <= i; j++)
            printf("%d", j);

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

        for (m = i; m >= 1; m--)
            printf("%d", m);

        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and read. Ask for n, then store it with scanf("%d", &n).

2. Same three-loop core. Only the source of n changes — the print logic matches Example 1.

3. Safer input tip. Unchecked scanf leaves n uninitialized on bad input. Prefer:

Safer input
if (scanf("%d", &n) != 1 || n < 1 || n > 9)
{
    printf("Enter a whole number from 1 to 9.\n");
    return 1;
}

Example 3 — Custom fill (##)

Keep the same loops; replace ** with another two-character center string.

C
#include <stdio.h>

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

    for (i = n; i >= 1; i--)
    {
        for (j = 1; j <= i; j++)
            printf("%d", j);

        for (k = i; k < n; k++)
            printf("##");

        for (m = i; m >= 1; m--)
            printf("%d", m);

        printf("\n");
    }

    return 0;
}

How It Works

1. Same structure. Outer countdown and digit loops are unchanged.

2. Swap the fill. Only the center printf changes — ## instead of **.

3. Learn stars first. Use Examples 1–2 for the classic pattern; treat fill swaps as a quick variation afterward.

Edge Cases & Pitfalls

Check these before calling the solution done.

single *

Broken symmetry

Printing one * per center step shrinks the middle by half and breaks the fixed 2n width. Keep printf("**") (two characters per iteration).

i++

Upside-down fill

If the outer loop counts up instead of down, stars shrink while digits grow. Use for (i = n; i >= 1; i--).

\n inside

Broken rows

If printf("\n") sits inside any inner loop, the mirror splits across lines. Call the newline only after all three loops.

n > 9

Multi-digit numbers

printf("%d", 10) prints two characters, so columns drift. Cap demos at 9 or format with fixed width.

n = 1

Single mirror

Output is just 11 — left 1 and right 1 with no stars. A good sanity check.

scanf

Check the return value

If scanf fails, n may be uninitialized — always test scanf(...) == 1.

Time and Space Complexity

ProgramTimeExtra space
Three loops + ** (Examples 1–2)O(n²)O(1)
Custom fill (Example 3)O(n²)O(1)

Each of n rows prints exactly 2n characters → total 2n² prints — quadratic in n.

Key Takeaways

  • Rule: print 1..i, then (n - i) pairs of **, then i..1.
  • Three loops: ascend, fill, descend — outer i counts down from n.
  • Break the row: call printf("\n") only after all three inner loops.
  • Complexity: O(n²) time from 2n² characters; O(1) extra space.

One line: for i from n down to 1, print 1..i, then ** pairs, then i..1, then printf("\n").

Frequently Asked Questions

Each iteration prints two asterisks so the center grows by 2 characters per row while keeping the pattern symmetric and each row exactly 2n characters wide.
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.
printf("%d", j) stays on the same line. printf("\n") ends the current line. Digits and stars use printf; the row break uses printf("\n") after all three inner loops.
Three — ascending digits (j), star pairs (k), and descending digits (m).
Yes. Replace "**" with two spaces or any two-character fill string (see Example 3).
O(n²) for size n because each of n rows prints 2n characters.
Check scanf's return value: if (scanf("%d", &n) != 1) handle bad input. Unchecked scanf leaves n uninitialized on failure.

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 at 2n characters.

Next: Centered Continuous Pyramid

Print a centered pyramid with a running number counter.

Program 24 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