Number Diamond Pattern in C#

What You’ll Learn
How to print a centered number diamond in C# by building a pyramid (top half) and then an inverted pyramid (bottom half).
Each row prints an odd count of digits: 1, 3, 5, 7, 9, and so on.
⭐ Pattern Output
For 5 levels, the pattern looks like this:
1
123
12345
1234567
123456789
1234567
12345
123
1Complete C# Program
Print the top pyramid from i = 1..5, then print the bottom pyramid from i = 4..1.
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();
}
}
}
}🧠 How It Works
Top half builds the pyramid
The first outer loop prints rows from 1 to 5.
Spaces center the row
The space loop prints indentation so the row is centered.
Odd-length number sequence
The number loop prints 1..(2*i-1) using k < i*2.
Bottom half mirrors the pyramid
The second outer loop runs from 4 down to 1 to print the inverted half.
Variation — User Input Levels
Let the user choose the number of levels (height of the top half).
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int levels;
Console.Write("Enter levels: ");
if (!int.TryParse(Console.ReadLine(), out levels) || levels <= 0)
{
Console.WriteLine("Please enter a positive integer.");
return;
}
for (int i = 1; i <= levels; i++)
{
for (int j = i; j < levels; j++)
Console.Write(" ");
for (int k = 1; k < i * 2; k++)
Console.Write(k);
Console.WriteLine();
}
for (int i = levels - 1; i >= 1; i--)
{
for (int j = levels; j > i; j--)
Console.Write(" ");
for (int k = 1; k < i * 2; k++)
Console.Write(k);
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Print the sequence in reverse (descending) to create a different diamond style
- Use
Console.Write(k + \" \")to add spacing between digits - Print stars instead of numbers to create a star diamond
Avoid
- Putting the newline inside the inner loops
- Using negative levels without validation
Key Takeaways
Top half increases from 1 to levels.
Bottom half decreases from levels-1 to 1.
Each row prints \(2i-1\) digits.
Indentation spaces keep the diamond centered.
Explore More C# Number Patterns!
Diamond patterns are a great way to practice centering logic with spaces and odd-length sequences.
Printing a diamond is usually just two triangles: an increasing triangle and a decreasing triangle joined together.
12 people found this page helpful
