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.
Approach
How to Solve It
Use a countdown outer loop and three inner loops — then optionally swap the center fill string.
Method
Idea
Best for
Three inner loops + **
Ascend, star pairs, descend
Learning, interviews, exams
Custom fill
Same 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
Goal
Pattern
Shrink digit range
for (i = n; i >= 1; i--)
Print 1..i
for (j = 1; j <= i; j++) printf("%d", j);
Print star pairs
for (k = i; k < n; k++) printf("**");
Print i..1
for (m = i; m >= 1; m--) printf("%d", m);
End the row
printf("\n");
Swap center fill
printf("##"); or printf(" ");
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d", j) / printf("**")
Stays on the same line
Digits and star pairs
printf("\n")
Ends the current line
After all three inner loops
Print the whole row without newlines, then end the row once.
Try it
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.
Trace each outer-loop value of i from 4 down to 1 and list the three parts of the row.
i
Left 1..i
Center
Right i..1
Printed row
4
1234
(none)
4321
12344321
3
123
**
321
123**321
2
12
****
21
12****21
1
1
******
1
1******1
Every row is 2 × 4 = 8 characters wide. Total characters for size n: n × 2n = 2n² — why time is O(n²).
Code
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;
}
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;
}
Output (when user enters 3)
Enter n: 3
123321
12**21
1****1
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;
}
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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.