A number & asterisk mirror prints ascending digits 1..i, a growing ** center, then descending digits i..1 — three inner loops on each row.
Remember
Rule: for i = n down to 1
print 1..i, then (n−i)×"**", then i..1
1234554321
1234**4321
123****321
12******21
1********1 ← n = 5
As i shrinks, the star center grows — a natural step after odd-length rows in Program 22.
Approach
How to Solve It
Count i down from n; print left digits, then ** pairs, then right digits.
Method
Idea
Best for
Three inner loops
Ascend, stars, descend
Learning, interviews
input() size
Same loops; read size at runtime
Interactive practice
Custom fill
Replace "**" with "##" etc.
Variant practice
Pseudocode
Pseudocode
for i from n down to 1:
for j from 1 to i:
print j (same line, no spaces)
for k from i to n - 1:
print "**" (same line)
for m from i down to 1:
print m (same line, no spaces)
print newline
Cheat sheet
Goal
Pattern
Set size
n = 5
Outer loop
for i in range(n, 0, -1):
Left digits
for j in range(1, i + 1): print(j, end="")
Center stars
for k in range(i, n): print("**", end="")
Right digits
for m in range(i, 0, -1): print(m, end="")
End row
print()
Custom fill
print("##", end="") in the center loop
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(..., end="")
Stays on the same line, no space
Digits and **
print()
Ends the current line
After all three inner loops
Glue digits and stars with end="", then break once with print(). Putting print() inside an inner loop splits the mirror across many lines.
Try it
Live Preview
Change size n and the mirror pattern updates instantly.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Trace each outer value of i, the left digits, star pairs, and right mirror.
i
Left 1..i
Stars
Right i..1
Printed row
3
123
none
321
123321
2
12
1× **
21
12**21
1
1
2× **
1
1****1
Star pairs = n − i. Digits shrink as the center grows — every row stays a balanced mirror.
Code
Python Programs
Three complete programs: fixed n = 5, input() size, and a custom center fill. Use View Output for sample results.
Example 1 — Fixed n = 5
Hard-coded size — three inner loops build left digits, **, then right digits.
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()
1. Outer countdown.i runs from n down to 1 — digit half-width for that row.
2. Left then center. Print 1..i, then print "**" exactly n − i times.
3. Right mirror. Print i..1, then print() ends the row.
Example 2 — input() Size
Read size at runtime; the three-loop structure stays the same.
Python
n = int(input("Enter the size: "))
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()
Output (when user enters 3)
Enter the size: 3
123321
12**21
1****1
How It Works
1. Read size. Convert the prompt answer with int().
2. Same three loops. Center star count still equals n − i for the chosen size.
3. Safer input tip. Bare int(input()) raises ValueError on letters. Prefer:
Safer input
raw = input("Enter the size: ").strip()
try:
n = int(raw)
except ValueError:
print("Enter a positive whole number.")
raise SystemExit(1)
if n < 1:
print("Enter a positive whole number.")
raise SystemExit(1)
Example 3 — Custom Fill ##
Keep n = 5 but replace asterisk pairs with hash pairs in the center.
Python
n = 5
fill = "##"
for i in range(n, 0, -1):
for j in range(1, i + 1):
print(j, end="")
for k in range(i, n):
print(fill, end="")
for m in range(i, 0, -1):
print(m, end="")
print()
1. Same structure. Outer and digit loops match Example 1.
2. Swap the fill. The center loop prints fill instead of "**".
3. Keep two characters. A two-character fill keeps the same visual width as **.
Edge Cases & Pitfalls
Check these before calling the solution done.
one *
Single asterisk
Printing "*" instead of "**" grows the center by 1 and breaks the even-width mirror.
print() inside
Broken rows
If print() is inside any inner loop, the mirror splits across lines. Call it only after all three loops.
range end
Wrong star count
Use range(i, n) (runs n − i times). Using range(i, n + 1) adds one extra pair.
n = 1
Single mirror
Output is just 11 — left 1, no stars, right 1.
n ≤ 0
Empty output
The outer loop never runs. Guard interactive input with n >= 1.
input()
Catch ValueError
Letters crash bare int(input()) — wrap in try/except and require n >= 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Examples 1–3
O(n²)
O(1)
Each of n rows prints about 2n characters of content → O(n²) total work.
Remember
Key Takeaways
Rule: print 1..i, then (n − i) pairs of **, then i..1.
Three loops: left digits, center fill, right mirror.
end= vs print(): glue with end=""; print() advances after each row.
Complexity:O(n²) for size n.
One line: shrink the digit halves each row, and grow a two-character center fill to keep the mirror balanced.
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 two-character fill (see Example 3).
O(n²) for size n because each row prints O(n) characters overall.
Use try/except ValueError around int(input()) 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.