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.
Approach
How to Solve It
One three-loop idea with two useful variants — star fill and custom fill.
Method
Idea
Best for
Three inner loops
Left 1..i, center ** pairs, right i..1
Learning, interviews, exams
Custom fill
Same loops; replace "**" with any string
Practice 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
Goal
Pattern
Shrink digit range
for (i = n; i >= 1; i--)
Left half
for (j = 1; j <= i; j++) Console.Write(j);
Star center
for (k = i; k < n; k++) Console.Write("**");
Right mirror
for (m = i; m >= 1; m--) Console.Write(m);
End the row
Console.WriteLine();
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each digit and each ** pair
Console.WriteLine
Ends the current line
After all three inner loops
Try it
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.
Trace each outer-loop value of i: left digits, star pairs, and right mirror.
i
Left / stars / right
Printed row
3
123 / — / 321
123321
2
12 / ** / 21
12**21
1
1 / **** / 1
1****1
Each row has width 2n characters when counting digits and star pairs together.
Code
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();
}
}
}
}
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();
}
}
}
}
Output (when user enters 3)
123321
12**21
1****1
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();
}
}
}
}
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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²).
Remember
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.