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 Python 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 in range(n, 0, -1) shrinks the digit range each row.

Three Inner Loops

j, k, m

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

print end= vs print()

Same line / next line

Digits and stars use print(..., end=""); end each row with print().

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 Python you use a descending outer loop, three inner loops for j, k, and m, then print() 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 in range(i, n) 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 print().

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

Python
# 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 nn = int(input(...))Flexible console programs
Custom fill"##" or " " instead of "**"Different center symbols

⚡ Quick Reference

GoalPattern
Walk rowsfor i in range(n, 0, -1)
Ascending digitsfor j in range(1, i + 1): print(j, end="")
Star centerfor k in range(i, n): print("**", end="")
Descending digitsfor m in range(i, 0, -1): print(m, end="")
End the rowprint()
User inputn = int(input(...))

📋 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 input() 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 Python 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.

Python
n = 5

for i in range(n, 0, -1):
    for j in range(1, i + 1):
        print(j, end="")

    for k in range(i, n):
        print("**", end="")

    for m in range(i, 0, -1):
        print(m, end="")

    print()

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. print() after all three inner loops starts the next row.

📈 User Input

Read the pattern size with input() instead of hard-coding 5.

Example 2 — User Input

Read n with input() and int(); all three loops use n as the bound.

Python
n = int(input("Enter n: "))

for i in range(n, 0, -1):
    for j in range(1, i + 1):
        print(j, end="")

    for k in range(i, n):
        print("**", end="")

    for m in range(i, 0, -1):
        print(m, end="")

    print()

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 raises ValueError from int(input()) — wrap it in try/except 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.

Python
n = 5

for i in range(n, 0, -1):
    for j in range(1, i + 1):
        print(j, end="")

    for k in range(i, n):
        print("##", end="")

    for m in range(i, 0, -1):
        print(m, end="")

    print()

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

print is built in; use input() when reading input. Set n and loop variables i, j, k, m.

Setup
2

Outer loop + descending i

for i in range(n, 0, -1) — each row prints fewer digits and more stars.

Row
3

Ascending digits (j)

for j in range(1, i + 1) then print(j, end="") — left half.

Left
4

Star pairs (k)

for k in range(i, n) then print("**", end="") — growing center.

Center
5

Descending digits (m)

for m in range(i, 0, -1) then print(m, end="") — right mirror.

Right
6

New line

print() 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 print(..., end="") vs row newline without complex math.

Example: put print() 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 try/except ValueError and positive-n checks.

Example: reject n <= 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 Python 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. Validate input()

    Wrap int(input()) in try/except ValueError so bad input does not crash the script.

  3. 3. Keep print() Outside

    Only call print() 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 print() 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 print(j, end="") and print("**", end=""); print() only after all three inner loops.

  2. 2. Skipping the Descending Loop

    Without for m in range(i, 0, -1) 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 in range(i, n) — exactly n - i pairs.

  4. 4. Forgetting the Row Break

    Omitting print() glues every number onto one endless line.

    → Always end the row after the inner loop.

  5. 5. Unchecked input

    Letters or empty input raise ValueError from int(input()).

    → Wrap int(input()) in try/except ValueError 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 input

int(input()) raises ValueError — validate with try/except first.

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.
  • print stays on the line; print() 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 print() 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 in range(n, 0, -1) in the outer loop
  • Run three inner loops: j, k, m
  • Print ** in the star loop — two chars per iteration
  • Wrap int(input()) in try/except ValueError before using n
  • State O(n²) time when asked about complexity

❌ Don’t

  • Call print() 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.
print(j, end="") stays on the same line with no trailing space. print() ends the current line. Digits and stars use end=""; the row break uses print() 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.
Use a try/except ValueError around int(input()) or check the raw string with .isdigit() before converting so bad input does not crash the script.
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 Python 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