A number-star diamond grows from 1 to n*n*…*n, then mirrors back down — each row alternates the row number and *.
Remember
Rule: top i = 1..n, bottom i = n-1..1
for j from 1 to 2*i-1:
odd j → print i even j → print *
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1 ← n = 5
Follows the right-aligned descending triangle in Program 30; next is the increasing triangle from 11 in Program 32.
Approach
How to Solve It
Two outer loops build the diamond. One shared inner rule: print 2*i−1 characters, alternating digit and star with j % 2.
Method
Idea
Best for
Two halves + modulus
Top 1..n, bottom n-1..1; even j → *
Learning, interviews
input() height
Same loops; conditional expression
Interactive practice
Compact n = 3
Same structure; easier to trace by hand
Paper walkthrough
Pseudocode
Pseudocode
for i from 1 to n:
for j from 1 to 2*i - 1:
if j % 2 == 0: print *
else: print i
print newline
for i from n - 1 down to 1:
for j from 1 to 2*i - 1:
if j % 2 == 0: print *
else: print i
print newline
Cheat sheet
Goal
Pattern
Top half
for i in range(1, n + 1):
Bottom half
for i in range(n - 1, 0, -1):
Row length
for j in range(1, i * 2): → 2*i-1 chars
Alternate
print("*" if j % 2 == 0 else i, end="")
End of row
print()
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(i, end="") / print("*", end="")
Stays on the same line
Each digit or star
print()
Ends the current line
After the inner loop
Glue characters with end="", then break once with print(). Putting print() inside the inner loop prints one character per line.
Try it
Live Preview
Change the height and the number-star diamond updates instantly — capped at 7 for readable demos.
Whole numbers from 1 to 7. Tap a chip or type a value — the preview redraws as you go.
Live resultn = 5 · 41 chars
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1
Trace
Worked Walkthrough — Top Half n = 5
Trace how each top-half row builds 2*i−1 characters. The bottom half repeats rows 4..1.
i
Chars
Pattern
Prints
1
1
i
1
2
3
i * i
2*2
3
5
i * i * i
3*3*3
4
7
i * … * i
4*4*4*4
5
9
peak
5*5*5*5*5
Top half = n² chars; bottom = (n−1)². Total lines = 2n − 1 → O(n²).
Code
Python Programs
Three complete programs: fixed n = 5, input() height, and compact n = 3. Use View Output for sample results.
Example 1 — Fixed n = 5
Hard-coded height — top half grows, bottom half mirrors, if/else on j % 2.
Python
for i in range(1, 6):
for j in range(1, i * 2):
if j % 2 == 0:
print("*", end="")
else:
print(i, end="")
print()
for i in range(4, 0, -1):
for j in range(1, i * 2):
if j % 2 == 0:
print("*", end="")
else:
print(i, end="")
print()
Output
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1
How It Works
1. Top half grows.i from 1 to 5 builds longer rows up to the peak.
2. Modulus alternates. Odd j prints i; even j prints *.
3. Bottom mirrors. Second loop starts at n - 1 so the peak is not printed twice.
Example 2 — input() Height
Read n at runtime; use a conditional expression for each position.
Python
n = int(input("Enter n: "))
if n < 1:
print("Please enter a positive integer.")
else:
for i in range(1, n + 1):
for j in range(1, i * 2):
print("*" if j % 2 == 0 else i, end="")
print()
for i in range(n - 1, 0, -1):
for j in range(1, i * 2):
print("*" if j % 2 == 0 else i, end="")
print()
Output (when user enters 3)
Enter n: 3
1
2*2
3*3*3
2*2
1
How It Works
1. Read and validate. Parse the answer; reject non-positive values before looping.
2. Same core. The conditional expression matches Example 1’s if/else — only n comes from the user.
3. Safer input tip. Bare int(input()) raises ValueError on letters. Prefer:
Safer input
raw = input("Enter n: ").strip()
try:
n = int(raw)
except ValueError:
print("Please enter a positive integer.")
raise SystemExit(1)
if n < 1:
print("Please enter a positive integer.")
raise SystemExit(1)
Example 3 — Compact n = 3
Same structure with a small peak — easy to confirm the bottom starts at n - 1.
Python
n = 3
for i in range(1, n + 1):
for j in range(1, i * 2):
if j % 2 == 0:
print("*", end="")
else:
print(i, end="")
print()
for i in range(n - 1, 0, -1):
for j in range(1, i * 2):
if j % 2 == 0:
print("*", end="")
else:
print(i, end="")
print()
Output
1
2*2
3*3*3
2*2
1
How It Works
1. Five lines. Peak 3*3*3 appears once; bottom starts at i = 2.
2. Trace on paper. If the bottom loop starts at n, the peak prints twice.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for height 5 or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
i = n
Doubled peak
Starting the bottom loop at n reprints the widest row. Use range(n - 1, 0, -1).
range bound
Wrong row length
range(1, i * 2 + 1) adds an extra character. Keep range(1, i * 2) for exactly 2*i-1 chars.
% flip
Stars in wrong spots
Even j must print * and odd j must print i — flipping them breaks the pattern.
print() inside
Broken rows
If print() sits inside the inner loop, you get one character per line. Call it only after the loop.
n = 1
Single digit
Output is just 1 — the bottom loop never runs. A good sanity check for input validation.
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)
Top half prints n² characters; bottom prints (n−1)². Total ≈ 2n² → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Two halves: top 1..n, bottom n-1..1 so the peak appears once.
Modulus rule: odd j → digit i; even j → *.
end= vs print(): characters stay on the line; print() advances after each row.
Complexity:O(n²) time from both halves; O(1) extra space.
One line: for each half, print 2*i−1 chars alternating i and *, then print().
Frequently Asked Questions
The inner loop runs for j in range(1, i * 2), which prints 1, 3, 5, 7, 9 characters for i = 1..5.
It checks j % 2. Even j prints '*', odd j prints the current row number i.
The first loop builds the top half (i = 1..n). The second mirrors back down (i = n-1..1) to complete the diamond.
Program 30 is a right-aligned descending triangle. Program 31 alternates digits and stars in a symmetric diamond shape.
Replace 5 with n in both outer loops — see Example 2.
O(n²) for height n because total printed characters grow as n² + (n-1)² across both halves.
Use try/except ValueError around int(input()) or check the raw string with .isdigit() before converting so bad input does not crash the script.
Only one row prints — a single 1. The bottom loop never runs.
Yes — print("*" if j % 2 == 0 else i, end="") compacts the if/else logic in Python.
🤔
Did you know?
This pattern prints a top half (1..n) and a bottom half (n-1..1). Each row prints 2*i-1 characters, alternating the row number and * using j % 2.