C# Number Mirror Pattern (Asterisk Center)

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

What Is This Pattern?

A number and asterisk mirror prints ascending digits, a growing star center, then the same digits descending — a left–right reflection on every row.

Remember
Rule: 1..i + ** pairs + i..1  (i = n..1)

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

In C# you use a descending outer loop and three inner loops for left digits, star pairs, and right digits, then Console.WriteLine() ends each row. Swap "**" for any two-character fill to customize the center.

How to Solve It

One three-loop idea with two useful variants — star fill and custom fill.

MethodIdeaBest for
Three inner loopsLeft 1..i, center ** pairs, right i..1Learning, interviews, exams
Custom fillSame loops; replace "**" with any stringPractice variants

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 halffor (j = 1; j <= i; j++) Console.Write(j);
Star centerfor (k = i; k < n; k++) Console.Write("**");
Right mirrorfor (m = i; m >= 1; m--) Console.Write(m);
End the rowConsole.WriteLine();

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach digit and each ** pair
Console.WriteLineEnds the current lineAfter all three inner loops

Live Preview

Change n and the mirror pattern updates instantly — including the total character count.

Whole numbers from 1 to 9 (keeps digits single-width). Tap a chip or type a value — the preview redraws as you go.

Live result n = 5 · 50 chars
1234554321
1234**4321
123****321
12******21
1********1

Worked Walkthrough — n = 3

Trace each outer-loop value of i: left digits, star pairs, and right mirror.

iLeft / stars / rightPrinted row
3123 / — / 321123321
212 / ** / 2112**21
11 / **** / 11****1

Each row has width 2n characters when counting digits and star pairs together.

C# Programs

Three complete programs: fixed size, user-input size, and a custom center fill. Use View Output for sample results.

Example 1 — Fixed n = 5

Three inner loops: ascending digits, star pairs, descending digits.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 5;
            int i, j, k, m;

            for (i = n; i >= 1; i--)
            {
                for (j = 1; j <= i; j++)
                {
                    Console.Write(j);
                }

                for (k = i; k < n; k++)
                {
                    Console.Write("**");
                }

                for (m = i; m >= 1; m--)
                {
                    Console.Write(m);
                }

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Shrink i. The outer loop counts down from 5 to 1 so each row loses one digit on each side.

2. Three parts. Left prints 1..i; center prints n - i pairs of **; right prints i..1.

3. Newline. WriteLine() after all three loops starts the next row.

Example 2 — User Input

Read n at runtime. Prefer int.TryParse in real apps so bad input does not throw.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int n;
            int i, j, k, m;

            Console.Write("Enter n: ");
            if (!int.TryParse(Console.ReadLine(), out n) || n < 1)
            {
                Console.WriteLine("Please enter a positive whole number.");
                return;
            }

            for (i = n; i >= 1; i--)
            {
                for (j = 1; j <= i; j++)
                {
                    Console.Write(j);
                }

                for (k = i; k < n; k++)
                {
                    Console.Write("**");
                }

                for (m = i; m >= 1; m--)
                {
                    Console.Write(m);
                }

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Validate n. TryParse rejects non-numeric input; require n >= 1.

2. Same three loops. For n = 3 the rows are 123321, 12**21, and 1****1.

Example 3 — Custom Fill ##

Same mirror shape — only the center fill string changes.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 5;
            int i, j, k, m;

            for (i = n; i >= 1; i--)
            {
                for (j = 1; j <= i; j++)
                {
                    Console.Write(j);
                }

                for (k = i; k < n; k++)
                {
                    Console.Write("##");
                }

                for (m = i; m >= 1; m--)
                {
                    Console.Write(m);
                }

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Swap the fill. Replace "**" with "##" (or two spaces, "--", etc.).

2. Same structure. Left and right digit loops are unchanged — only the center changes.

Edge Cases & Pitfalls

Check these before calling the solution done.

missing mirror

Skip the descending loop

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

star bound

k <= n instead of k < n

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

single *

Write("*") instead of "**"

One star per iteration breaks the even-width center. Prefer two characters per pair.

WriteLine early

WriteLine inside an inner loop

That splits the row. Call WriteLine() only after all three inner loops finish.

n = 1

Single 11

Output is 11 — left 1 and right 1, no stars.

Bad input

Convert.ToInt32 throws

Prefer int.TryParse so non-numeric input does not crash the program.

Time and Space Complexity

ProgramTimeExtra space
Star fill (Examples 1–2)O(n²)O(1)
Custom fill (Example 3)O(n²)O(1)

There are n rows; each prints about 2n characters, so total work is O(n²).

Key Takeaways

  • Rule: print 1..i, then n - i pairs of **, then i..1.
  • Count down: the outer loop starts at n so digits shrink and stars grow.
  • Write vs WriteLine: digits and stars stay on the line; WriteLine advances after each row.
  • Next step: Program 24 builds a centered continuous number pyramid.

One line: for i = n..1, print 1..i, then star pairs, then i..1, then WriteLine.

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.
Console.Write stays on the same line. Console.WriteLine ends the current line. Digits and stars use Write; the row break uses WriteLine 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.
Prefer int.TryParse(Console.ReadLine(), out n) so bad input does not throw FormatException.
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.

Next: Centered Continuous Number Pyramid

Center a continuous counter under leading spaces for a pyramid look.

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