A number diamond prints ascending digits in odd-length rows that grow to a peak, then shrink back — centered with leading spaces.
Remember
Rule: top 1..levels, bottom levels-1..1; each row prints 1..(2*i-1)
1
123
12345
1234567
123456789
1234567
12345
123
1 ← levels = 5
Follows the right-aligned triangle in Program 43; next is the X pattern of stars and zeros in Program 45.
Approach
How to Solve It
Build the top half, then mirror it downward — spaces first, then digits.
Method
Idea
Best for
Two-half diamond
Top 1..levels, bottom levels-1..1
Learning, interviews, exams
Levels input
Same logic with a user-chosen levels
Practice / demos
Pseudocode
Pseudocode
for i from 1 to levels:
print (levels - i) spaces
print digits 1 to (2*i - 1)
print newline
for i from levels-1 down to 1:
print (levels - i) spaces
print digits 1 to (2*i - 1)
print newline
Cheat sheet
Goal
Pattern
Top half
for i in range(1, levels + 1):
Bottom half
for i in range(levels - 1, 0, -1):
Top indent
for j in range(i, levels): print(" ", end="")
Bottom indent
for j in range(levels, i, -1): print(" ", end="")
Digits
for k in range(1, i * 2): print(k, end="")
End the row
print()
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(" ", end="") / print(k, end="")
Stays on the same line
Each space or digit
print()
Ends the current line
After spaces and digits on a row
Print spaces and digits without a newline, then end the row once.
Try it
Live Preview
Change the level count and the number diamond updates instantly.
Whole numbers from 1 to 5 (peak stays within single digits 1–9). Tap a chip or type a value — the preview redraws as you go.
Live result5 levels · 9 lines
1
123
12345
1234567
123456789
1234567
12345
123
1
Trace
Worked Walkthrough — levels = 3
Trace spaces and digits on the way up, then back down.
Half
i
Spaces / digits
Printed row
Top
1
2 / 1
1
Top
2
1 / 123
123
Top
3
0 / 12345
12345
Bottom
2
1 / 123
123
Bottom
1
2 / 1
1
Total lines = 2 * levels − 1. Digit count on row i = 2 * i − 1.
Code
Python Programs
Three complete programs: fixed 5 levels, input() levels, and a compact 3-level demo. Use View Output for sample results.
Example 1 — Fixed levels = 5
Top half grows to 123456789; bottom half mirrors back to 1.
Python
levels = 5
for i in range(1, levels + 1):
for j in range(i, levels):
print(" ", end="")
for k in range(1, i * 2):
print(k, end="")
print()
for i in range(levels - 1, 0, -1):
for j in range(levels, i, -1):
print(" ", end="")
for k in range(1, i * 2):
print(k, end="")
print()
Output
1
123
12345
1234567
123456789
1234567
12345
123
1
How It Works
1. Top half.i grows from 1 to 5; each row prints levels − i spaces, then digits 1..(2*i−1).
2. Bottom half.i shrinks from 4 to 1 so the peak row is not printed twice.
3. Newline. Bare print() after spaces and digits starts the next row.
Example 2 — User Input Levels
Read the level count with input() and build the same diamond.
Python
try:
levels = int(input("Enter levels: "))
except ValueError:
print("Please enter a positive whole number.")
raise SystemExit(1)
if levels < 1:
print("Please enter a positive whole number.")
raise SystemExit(1)
for i in range(1, levels + 1):
for j in range(i, levels):
print(" ", end="")
for k in range(1, i * 2):
print(k, end="")
print()
for i in range(levels - 1, 0, -1):
for j in range(levels, i, -1):
print(" ", end="")
for k in range(1, i * 2):
print(k, end="")
print()
2. Same diamond. Only the bound changes from the literal 5 to the user value.
3. Safer input tip. Keep single-digit peaks for readable demos:
Safer input tip
if levels < 1 or levels > 5:
print("Enter a whole number from 1 to 5.")
raise SystemExit(1)
Example 3 — Compact levels = 3
Same algorithm with a smaller peak — easier to trace by hand.
Python
levels = 3
for i in range(1, levels + 1):
for j in range(i, levels):
print(" ", end="")
for k in range(1, i * 2):
print(k, end="")
print()
for i in range(levels - 1, 0, -1):
for j in range(levels, i, -1):
print(" ", end="")
for k in range(1, i * 2):
print(k, end="")
print()
Output
1
123
12345
123
1
How It Works
1. Same rules. Only levels changes — loops and digit math stay identical.
2. Quick check. Five lines total; the middle row is 12345.
Edge Cases & Pitfalls
Check these before calling the solution done.
peak twice
Bottom loop starts at levels
The widest row prints twice. Start the bottom at levels - 1.
no spaces
Forget the space loops
Rows left-align and the diamond shape disappears. Keep levels − i spaces.
k <= i
Print only to i
Rows become 1, 12, 123… instead of odd lengths. Use range(1, i * 2) (or range(1, 2 * i)).
levels > 5
Digits past 9
print(k) prints 10, 11… and columns break. Cap at 5 for single-digit rows, or format with fixed width.
levels = 1
Single cell
Output is just 1 — bottom loop never runs.
input()
Catch ValueError
Bare int(input()) crashes on non-numeric text — wrap it in try/except ValueError.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / compact (Examples 1, 3)
O(n²)
O(1)
User input (Example 2)
O(n²)
O(1)
There are 2n − 1 rows and up to O(n) digits per row, so total work is quadratic in the level count.
Remember
Key Takeaways
Rule: grow i to levels, then shrink; each row prints 1..(2*i−1).
Center: print levels − i spaces before the digits on every row.
end="" vs print(): spaces and digits stay on the line; bare print() advances after each row.
Complexity:O(n²) time; O(1) extra space.
One line: indent, print an odd-length ascending digit row, grow to the peak, then mirror down.
Frequently Asked Questions
A centered diamond of ascending digits: 1, 123, 12345, 1234567, 123456789, then the same rows mirrored back down to 1.
The row prints 2*i-1 digits (odd length): 1, 3, 5, 7, 9, … using for k in range(1, i * 2).
A space loop prints leading spaces before the digits. Top half uses for j in range(i, levels); bottom half uses for j in range(levels, i, -1).
The first builds the top half (i = 1..levels). The second mirrors back down (i = levels-1..1) to complete the diamond.
Program 43 is a right-aligned triangle with fixed-width columns. Program 44 is a symmetric centered diamond with odd-length digit rows.
Replace 5 with a levels variable in both outer loops and space bounds — see Example 2.
O(n²) for n levels because each level prints O(n) digits and there are O(n) rows overall.
Use try/except ValueError around int(input()) and require levels >= 1 — see Example 2.
Only one row prints — a single centered 1.
🤔
Did you know?
Top half runs i = 1..levels; bottom half mirrors i = levels-1..1. Each row prints 2*i-1 digits (1 to 2*i-1) with leading spaces to center the diamond.