Shape Rule
Diamond halves
Top half grows 1..n; bottom half mirrors n-1..1.

The number-star diamond prints 1, 2*2, 3*3*3, … 5*5*5*5*5, then mirrors back down — a natural step after the right-aligned triangle in Program 30. This tutorial covers two outer loops, modulus alternation, a live preview, worked Python examples, edge cases, and complexity.
Diamond halves
Top half grows 1..n; bottom half mirrors n-1..1.
i = 1..n
for i in range(1, n + 1): — builds the growing half of the diamond.
i = n-1..1
for i in range(n - 1, 0, -1): — mirrors the top half back down.
Alternate fill
Odd j prints i; even j prints *.
Height 3–7
Pick a height and draw the number-star diamond in the browser.
Complexity
Each row prints 2*i-1 chars — total work scales as n².
A number-star diamond pattern alternates the row number and * on each line, growing to a peak then mirroring back down. With n = 5, you get 1, 2*2, … 5*5*5*5*5, then the same rows in reverse.
In Python you use two outer loops (top and bottom halves) and j % 2 inside the inner loop to alternate digit and star.
It combines symmetric diamond logic with the modulus operator — a step up from Program 30’s single-loop triangle.
Inner loop runs j < i*2.
Odd prints i, even prints *.
Top 1..n, bottom n-1..1.
Follow Program 30; continue to Program 32 (triangle from 11) next.
In short: top loop i = 1..n, bottom loop i = n-1..1, inner j % 2 alternates digit and star, then print().
Given n = 5, print a number-star diamond: top half i = 1..n, bottom half i = n-1..1, each row alternating digit i and * via j % 2.
# n = 5 (conceptual shape)
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() | Item | Type | Description |
|---|---|---|
n | int | Diamond peak height — total lines = 2*n - 1. |
i | int | Outer loop — current row number printed on odd positions. |
j | int | Inner loop — j % 2 == 0 prints *, else prints i. |
for i from 1 to n:
for j from 1 to i*2 - 1:
if j % 2 == 0: print *
else: print i
print newline
for i from n-1 down to 1:
for j from 1 to i*2 - 1:
if j % 2 == 0: print *
else: print i
print newline | Approach | Idea | Best for |
|---|---|---|
| if/else | 1, 2*2, 3*3*3, … | Learning and interviews |
| Conditional expression | print("*" if j % 2 == 0 else i, end="") | Compact console programs |
| User-input n | int(input(...)) | Flexible diamond height |
| Goal | Pattern |
|---|---|
| Top half | for i in range(1, n + 1): |
| Bottom half | for i in range(n - 1, 0, -1): |
| Inner loop | for j in range(1, i * 2): |
| Alternate fill | if j % 2 == 0: print("*", end="") else: print(i, end="") |
| Conditional form | print("*" if j % 2 == 0 else i, end="") |
| User input | int(input(...)) |
Same number-star diamond — different ways to write the modulus check and control height.
i = 1..nGrowing rows to the peak
i = n-1..1Mirror back down
"*" if j%2==0 else iAlternate star and digit
2*i-1Characters per row
Reach for this pattern when teaching symmetric diamonds, the modulus operator, and two-phase loop structures.
Natural follow-up after Program 30 — introduces modulus and a mirrored bottom half.
Outer/inner bound practice with an immediate visual check.
Combine loops with input() for a flexible row count.
Compare Program 30 (right-aligned triangle) and Program 32 (triangle from 11) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a height between 3 and 7 and draw the number-star diamond in the browser.
Three complete Python programs — fixed height, user input with conditional expression form, and a smaller trace demo. Click View Output to reveal sample console results.
Print a full diamond with n = 5 using if/else and j % 2.
n = 5Hard-coded row count — ideal for first demos and screenshots.
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() When i = 1, the inner loop prints one character — 1. When i = 3, it prints 3*3*3 (five characters). The bottom half mirrors from i = 4 down to 1.
Read the diamond height with input() instead of hard-coding 5.
Read n with input() and int() to control diamond height.
n = int(input("Enter n: "))
if n < 1:
raise SystemExit
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() Same diamond core as Example 1; a conditional expression replaces if/else and n replaces hard-coded 5. Non-numeric input raises ValueError with bare int(input()) — use try/except for safer labs.
Run with n = 3 to trace every row on paper before scaling up.
n = 3Same if/else logic with a smaller row count for quick tracing.
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() Only n changes from 5 to 3 — the if/else and two-loop structure stay identical. Trace i = 1, 2, 3 on paper to see how row length grows as 2*i-1.
No imports needed for fixed height; use input() when reading. Set n = 5 and loop variables i, j.
for i in range(1, n + 1): — growing rows from 1 to the peak.
for j in range(1, i * 2): — prints 2*i-1 characters per row.
j % 2 == 0 prints *; odd j prints i.
for i in range(n - 1, 0, -1): — mirrors the top half back down.
2*n-1 total rows — O(n²) time, O(1) extra memory.
n = 5Trace each outer-loop value of i, inner-loop range, character count, and full row output.
i | Inner range (j) | Chars | Row output |
|---|---|---|---|
1 | 1 | 1 | 1 |
2 | 1, 2, 3 | 3 | 2*2 |
3 | 1..5 | 5 | 3*3*3 |
4 | 1..7 | 7 | 4*4*4*4 |
5 | 1..9 | 9 | 5*5*5*5*5 |
Characters per row = 2*i-1. Bottom half repeats rows 4, 3, 2, 1 in reverse.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: flip j % 2 logic and watch stars land on wrong positions.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 32 for a triangle starting from 11.
Practice print vs row newline without complex math.
Example: put print() inside the inner loop by mistake.
Add spaces between digits once the two-loop structure works.
Example: use print(i, end=" ") between digits for wider spacing.
Triangular totals make O(n²) concrete for beginners.
Example: count printed characters for n = 5 — top half alone prints 25 chars.
Pair the pattern with input() return checks and positive-row checks.
Example: reject max <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace i and j on paper for n = 3 before coding — watch how row length grows as 2*i-1.
Small habits that keep number-pattern code clean.
Top half 1..n and bottom half n-1..1 — do not repeat the peak row.
try/except ValueErrorUse try/except ValueError so bad input does not crash when converting n.
Only call print() after the inner loop finishes the row.
Mark odd/even positions for each row before coding the alternation.
Trace i = 1..3 on paper before coding the full n = 5 demo.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put print() inside the inner loop.
Mistakes that commonly break number-star diamond patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use print(i, end="") or print("*", end=""); print() only after the inner loop.
Using j % 2 != 0 for stars (instead of == 0) swaps digit and star positions.
→ Even j prints *; odd j prints i.
j <= i * 2 adds an extra character — row length becomes even instead of odd.
→ Keep for j in range(1, i * 2): for exactly 2*i-1 chars.
Starting the bottom loop at i = n prints the widest row twice.
→ Bottom half starts at i = n - 1, not n.
int(input())Letters or empty input raise ValueError with bare int(input()).
→ Catch ValueError and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 — one row, no bottom half needed.
Outer loop never runs — print nothing or show a message.
n < 0Treat as invalid; re-prompt instead of silent empty output.
Three rows: 1, 2*2, 1.
Bare int(input()) raises ValueError on bad input — use try/except first.
Total lines = 2*n - 1 — grows quadratically with peak height.
Try these variations to lock in the pattern.
j > ii = 1..n without the mirror* with # or .j % 2 logic, different symbolj prints i; even j prints *. Inner loop runs j < i*2.print(..., end="") stays on the line; print() advances — mix them carefully.n > 0 for interactive programs; n = 1 prints a single 1.n - 1 — do not repeat the peak row at i = n.Quick Takeaway: top loop i = 1..n, bottom i = n-1..1, inner j % 2 alternates digit and star, then print().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The number-star diamond is a compact lesson in symmetric patterns and the modulus operator: alternate i and * with j % 2, grow rows in the top half, then mirror back down. Master the fixed-n version, then try user input and a smaller trace demo.
Practice the three examples above, then continue to Program 32 for the increasing number triangle starting from 11.
Bottom half must start at n - 1 — validate n when reading from the console.
for i in range(1, n + 1):for i in range(n - 1, 0, -1):j % 2 == 0 prints *, else prints iint(input()) in try/except ValueErrorprint() inside the inner loopi = n (repeats peak row)j <= i * 2 instead of j < i * 2n = 1 edge casePrint the pattern the beginner-friendly way.
j%2: * or i
DefinitionTop + mirror
Code2*i-1 chars
Codei = n-1
ShapeO(n²) time
AnalysisThis 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.
Move on to the increasing number triangle starting from 11 in the Python number-pattern series.
12 people found this page helpful