Number & Asterisk Mirror Pattern in C#

Beginner
⏱️ 8 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Three Inner Loops

What You’ll Learn

The number & asterisk mirror pattern prints 1..i, a growing ** center, then i..1 — a natural step after odd-length rows in Program 22. This tutorial covers the shape rule, three inner loops, a live preview, algorithm steps, worked C examples, edge cases, and complexity.

Shape Rule

Mirror + stars

Row 1 prints 1234554321, row 2 prints 1234**4321, row 5 prints 1********1.

Outer Loop

n..1

for (i = n; i >= 1; i--) shrinks the digit range each row.

Three Inner Loops

j, k, m

Ascending 1..i, star pairs **, descending i..1.

printf vs newline

Same line / next line

Digits and stars use printf; end each row with printf("\n").

Live Preview

1–9 size

Pick a size n and draw the mirror pattern instantly in the browser.

O(n²)

Complexity

Each row prints O(n) characters; total work scales as .

Introduction

A number & asterisk mirror pattern prints ascending digits, a growing star center, then descending digits on each row. With n = 5, the output is 1234554321, 1234**4321, 123****321, 12******21, 1********1.

In C you use a descending outer loop, three inner loops for j, k, and m, then printf("\n") ends each row.

Why it matters?

It combines three inner loops with symmetry — a step up from Program 22’s single inner loop.

Key Highlights

Left 1..i

First inner loop prints ascending digits.

Center **

for (k = i; k < n; k++) prints star pairs.

Right i..1

Third loop mirrors the left half descending.

Series Foundation

Follow Program 22; continue to Program 24 (centered pyramid) next.

In short: for each i from n down to 1, print 1..i, then ** pairs, then i..1, then printf("\n").

📝 Problem & Approach

Given a positive integer n, print a mirror pattern: for each i from n down to 1, print digits 1..i, then (n - i) pairs of **, then digits i..1.

c
// n = 5 (conceptual shape)
// 1234554321
// 1234**4321
// 123****321
// 12******21
// 1********1

Inputs & Outputs

ItemTypeDescription
nintPattern size — outer loop runs from n down to 1.
jintAscending loop — prints 1..i.
kintStar loop — prints ** for k = i..n-1.
mintDescending loop — prints i..1 to mirror the left.

Minimal workflow

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

Approach comparison

ApproachIdeaBest for
Three inner loops1234554321, 1234**4321, …Learning and interviews
User-input nscanf("%d", &n);Flexible console programs
Custom fill"##" or " " instead of "**"Different center symbols

⚡ Quick Reference

GoalPattern
Walk rowsfor (i = n; i >= 1; i--)
Ascending digitsfor (j = 1; j <= i; j++) printf("%d", j);
Star centerfor (k = i; k < n; k++) printf("**");
Descending digitsfor (m = i; m >= 1; m--) printf("%d", m);
End the rowprintf("\n");
User inputscanf("%d", &n);

📋 Fixed n vs User Input vs Custom Fill

Same mirror pattern — different ways to control size and center symbol.

Left half
1..i

Ascending digits in first inner loop

Center
**

Star pairs grow as i shrinks

Right half
i..1

Descending digits mirror the left

Learning tip
3 loops

j ascending, k stars, m descending

Context

When This Pattern Shows Up

Reach for this pattern when teaching symmetry, multiple inner loops, and mixed character output in nested loops.

  1. Post odd-rows exercise

    Natural follow-up after Program 22 — introduces three inner loops and symmetry.

  2. Nested-loop warm-up

    Outer/inner bound practice with an immediate visual check.

  3. Console I/O practice

    Combine loops with scanf for a flexible row count.

  4. Gateway to variants

    Compare Program 22 (odd-length rows) and Program 24 (centered pyramid) next.

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.

🔮 Live Preview

Choose a size between 1 and 9 and draw the number & asterisk mirror pattern in the browser.

Try 3, 5, or 7. Max up to 9 in this preview.

Live result
Press "Draw pattern".

Examples Gallery

Three complete C programs — fixed size, user input, and custom center fill. Click View Output to reveal sample console results.

📚 Getting Started

Print five rows of the mirror pattern with three inner loops.

Example 1 — Fixed n = 5

Hard-coded size — ideal for first demos and screenshots.

c
#include <stdio.h>

int main() {
    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

When i = 5, print 12345, no stars, then 54321 — full mirror with no center fill. When i = 3, print 123, two ** pairs, then 321 — output 123****321. printf("\n") after all three inner loops starts the next row.

📈 User Input

Read the pattern size with scanf instead of hard-coding 5.

Example 2 — User Input

Read n with scanf("%d", &n); all three loops use n as the bound.

c
#include <stdio.h>

int main() {
    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

Same three-loop core as Example 1; only the source of n changes. The star loop bound k < n scales with the user’s input. Non-numeric input leaves n unset if you ignore scanf’s return value — always check it in safer labs.

⚡ Custom Fill

Replace ** with another two-character fill string.

Example 3 — Custom Fill ##

Keep n = 5 but use hash pairs instead of asterisks in the center.

c
#include <stdio.h>

int main() {
    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

Replace only "**" with "##" in the star loop — digit loops stay the same. Any two-character string works as center fill.

🧠 How the Algorithm Prints Rows

1

Set up

#include <stdio.h> brings in printf / scanf. Set n and loop variables i, j, k, m.

Setup
2

Outer loop + descending i

for (i = n; i >= 1; i--) — each row prints fewer digits and more stars.

Row
3

Ascending digits (j)

for (j = 1; j <= i; j++) then printf("%d", j) — left half.

Left
4

Star pairs (k)

for (k = i; k < n; k++) then printf("**") — growing center.

Center
5

Descending digits (m)

for (m = i; m >= 1; m--) then printf("%d", m) — right mirror.

Right
6

New line

printf("\n") ends the row after all three inner loops.

Break
=

Symmetric mirror complete

Each row stays symmetric — O(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — n = 5

Trace each outer-loop value of i, star count, and the full row output.

iLeft 1..iStar pairsRight i..1Row output
5123450543211234554321
412341 (× **)43211234**4321
31232321123****321
21232112******21
11411********1

Star pairs per row = n - i — grows as digits shrink.

Use Cases

Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.

1. Teaching Nested Loops

Clearest visual proof that outer and inner bounds interact.

Example: change j <= i and watch the shape change.

2. Pattern Series Base

Foundation for inverted, pyramid, diamond, and hollow variants.

Example: use (i + j) % 2 for row+column parity grids.

3. Console Formatting Drills

Practice printf vs row newline without complex math.

Example: put printf("\n") inside the inner loop by mistake.

4. Character Substitution

Swap digits for letters, stars, or spaced output once the loop works.

Example: print j + " " for spaced digits on each row.

5. Complexity Intuition

Triangular totals make O(n²) concrete for beginners.

Example: count printed digits for n = 10 still → 55.

6. Input Validation Labs

Pair the pattern with scanf return checks and positive-row checks.

Example: reject max <= 0 and re-prompt.

Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.

Advantages

Why this pattern earns a permanent spot in beginner C courses.

  1. 1. Instant Visual Feedback

    Wrong bounds show up immediately as a broken staircase.

  2. 2. Minimal Concepts

    Only loops and console output — no arrays or math libraries.

  3. 3. Easy to Extend

    Invert, center, hollow, or change the fill character with small edits.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters.

Pro Tip: trace all three inner loops on paper for n = 3 before coding — symmetry bugs hide in loop bounds.

Usage Tips

Small habits that keep number-pattern code clean.

  1. 1. Name Bounds Clearly

    Do not skip the descending m loop — without it you lose the mirror.

  2. 2. Prefer scanf

    Check the return value so bad input does not leave n uninitialized.

  3. 3. Keep printf("\n") Outside

    Only call printf("\n") after the inner loop finishes the row.

  4. 4. Trace j, k, m on Paper

    Write each i, star count, and mirror half before coding.

  5. 5. Dry-Run One Small n

    Trace n = 3 on paper before coding larger demos.

Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put printf("\n") inside the inner loop.

Common Pitfalls

Mistakes that commonly break mirror number patterns.

  1. 1. Newline Inside the Inner Loop

    Each digit lands on its own line — you get a column, not a triangle.

    → Use printf("%d", j) and printf("**"); printf("\n") only after all three inner loops.

  2. 2. Skipping the Descending Loop

    Without for (m = i; m >= 1; m--) the row is not mirrored.

    → Always print i..1 after the star loop.

  3. 3. Wrong Star Loop Bound

    Using k <= n prints one extra star pair per row.

    → Use for (k = i; k < n; k++) — exactly n - i pairs.

  4. 4. Forgetting the Row Break

    Omitting printf("\n") glues every number onto one endless line.

    → Always end the row after the inner loop.

  5. 5. Unchecked scanf

    Letters or empty input leave n uninitialized.

    → Check scanf return value and re-prompt on failure.

Edge Cases

Check these inputs before calling the solution done.

n = 1

Single row

Output is just 11 — one digit each side, no stars.

n = 0

Empty pattern

Outer loop never runs — print nothing or show a message.

Negative

n < 0

Treat as invalid; re-prompt instead of silent empty output.

Large n

Large width

Output grows as n² characters — fine for labs, noisy for huge n.

Bad input

Non-numeric scanf input

Unchecked scanf leaves n unset — check the return value.

n = 2

Smallest mirror

Two rows: 1221 and 1**1.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Odd-length rows

2. Centered pyramid

  • Continuous counter with spacing
  • Continue with Program 24

3. Single-star center

  • Replace ** with * — slower center growth
  • Compare row widths side by side

4. No trailing space

  • Print space only between numbers, not after the last
  • Harder follow-up after this page

Notes

  • Symmetry. Left 1..i + right i..1 with star fill — row width stays consistent.
  • printf stays on the line; printf("\n") advances — mix them carefully.
  • Validate n > 0 for interactive programs; n = 1 prints 11.
  • This page is left-aligned. Centered pyramids need leading spaces — covered later in the series.

Quick Takeaway: descending outer loop, three inner loops (j, k, m), then printf("\n") after each row.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(n²)O(1)
Custom fill (Example 3)O(n²)O(1)
Wrap Up

🎉 Conclusion

The number & asterisk mirror pattern is a compact lesson in symmetry: print 1..i, star pairs, then i..1 with three inner loops. Master the fixed-n version, then try user input and a custom center fill.

Practice the three examples above, then continue to Program 24 for the centered continuous number pyramid.

Never skip the descending m loop — validate n when reading from the console.

💡 Best Practices

✅ Do

  • Use for (i = n; i >= 1; i--) in the outer loop
  • Run three inner loops: j, k, m
  • Print ** in the star loop — two chars per iteration
  • Check scanf return value before using n
  • State O(n²) time when asked about complexity

❌ Don’t

  • Call printf("\n") inside any inner loop
  • Skip the descending m loop
  • Use k <= n in the star loop
  • Forget n in the star bound — hard-code 5 in Example 2 style only for demos
  • Ignore bad console input in user-facing demos
  • Skip the n = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this mirror pattern

Print the pattern the beginner-friendly way.

5
Core concepts
02

Outer i--

n down to 1

Code
+ 03

3 loops

j, k, m

Code
04

Star fill

n - i pairs

Shape
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

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.
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 fill string (see Example 3).
O(n²) for size n because each row prints O(n) characters overall.
Check scanf's return value: if (scanf("%d", &n) != 1) handle bad input. Unchecked scanf leaves n uninitialized on failure.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

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.

Continue to Program 24

Move on to the centered continuous number pyramid in the C number-pattern series.

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