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
In C# use two outer loops: grow i to levels, then shrink. Indent with spaces, then print k from 1 while k < i * 2.
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 = 1; i <= levels; i++)
Bottom half
for (i = levels - 1; i >= 1; i--)
Top indent
for (j = i; j < levels; j++) Console.Write(" ");
Bottom indent
for (j = levels; j > i; j--) Console.Write(" ");
Digits
for (k = 1; k < i * 2; k++) Console.Write(k);
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each space or digit
Console.WriteLine
Ends the current line
After spaces and digits on a row
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
C# Programs
Three complete programs: fixed 5 levels, user-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.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j, k;
for (i = 1; i <= 5; i++)
{
for (j = i; j < 5; j++)
Console.Write(" ");
for (k = 1; k < i * 2; k++)
Console.Write(k);
Console.WriteLine();
}
for (i = 4; i >= 1; i--)
{
for (j = 5; j > i; j--)
Console.Write(" ");
for (k = 1; k < i * 2; k++)
Console.Write(k);
Console.WriteLine();
}
}
}
}
Output
1
123
12345
1234567
123456789
1234567
12345
123
1
How It Works
1. Top half.i runs 1..5; print 5 - i spaces, then digits while k < i * 2.
2. Bottom half.i runs 4..1 with the same digit rule so the shape mirrors.
3. Peak. When i = 5, digits are 1 through 9 — the widest row.
Example 2 — User Input (levels)
Read the level count and build both halves from that value.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int levels, i, j, k;
Console.Write("Enter levels: ");
if (!int.TryParse(Console.ReadLine(), out levels) || levels < 1)
{
Console.WriteLine("Please enter a positive whole number.");
return;
}
for (i = 1; i <= levels; i++)
{
for (j = i; j < levels; j++)
Console.Write(" ");
for (k = 1; k < i * 2; k++)
Console.Write(k);
Console.WriteLine();
}
for (i = levels - 1; i >= 1; i--)
{
for (j = levels; j > i; j--)
Console.Write(" ");
for (k = 1; k < i * 2; k++)
Console.Write(k);
Console.WriteLine();
}
}
}
}
2. Same diamond. Both outer loops and space bounds use levels instead of the literal 5.
Example 3 — Compact levels = 3
A smaller fixed demo — easier to trace by hand before scaling up.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int levels = 3;
int i, j, k;
for (i = 1; i <= levels; i++)
{
for (j = i; j < levels; j++)
Console.Write(" ");
for (k = 1; k < i * 2; k++)
Console.Write(k);
Console.WriteLine();
}
for (i = levels - 1; i >= 1; i--)
{
for (j = levels; j > i; j--)
Console.Write(" ");
for (k = 1; k < i * 2; k++)
Console.Write(k);
Console.WriteLine();
}
}
}
}
Output
1
123
12345
123
1
How It Works
1. Same rules. Grow, then shrink; spaces then digits on every row.
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 k < i * 2 (or k <= 2*i - 1).
levels > 5
Digits past 9
Console.Write(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.
Bad input
Convert.ToInt32 throws
Prefer int.TryParse so non-numeric input does not crash the program.
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.
Write vs WriteLine: spaces and digits stay on the line; WriteLine advances after each row.
Next step: Program 45 prints an X of stars and zeros on a grid.
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 = 1; k < i*2; k++).
A space loop prints leading spaces before the digits. Top half uses for (j = i; j < levels; j++); bottom half uses for (j = levels; j > i; j--).
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.
Prefer int.TryParse(Console.ReadLine(), out levels) so bad input does not throw FormatException.
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.