Mirror Diagonal Diamond Pattern in C#

What You’ll Learn
How to print a mirror diagonal diamond number pattern in C# by printing the top half first and then mirroring it to form the bottom half.
This program is a practical way to learn nested loops, conditional printing, and symmetry in console patterns.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1 1\n 2 2\n 3 3\n 4 4\n 5\n 4 4\n 3 3\n 2 2\n1 1Complete C# Program
First print the top half (1..rows), then print the bottom half (rows-1..1) to mirror the shape.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= rows; j++)
{
if (i == j)
Console.Write(j);
else
Console.Write(" ");
}
for (int k = rows - 1; k >= 1; k--)
{
if (i == k)
Console.Write(k);
else
Console.Write(" ");
}
Console.WriteLine();
}
for (int i = rows - 1; i >= 1; i--)
{
for (int j = 1; j <= rows; j++)
{
if (i == j)
Console.Write(j);
else
Console.Write(" ");
}
for (int k = rows - 1; k >= 1; k--)
{
if (i == k)
Console.Write(k);
else
Console.Write(" ");
}
Console.WriteLine();
}
}
}
}🧠 How It Works
Choose the number of rows
int rows = 5; sets the diamond height (which becomes 2*rows - 1 lines).
Print the top half
for (i = 1; i <= rows; i++) prints the upper part from 1 to rows.
Print the left + mirrored diagonals
The two inner loops print numbers only when indices match (i == j and i == k). Otherwise, they print spaces to keep alignment.
Mirror the bottom half
for (i = rows - 1; i >= 1; i--) reprints the same diagonal rows in reverse order, which forms the diamond.
Diamond via symmetry
There are \(2 \cdot rows - 1\) lines, and each line prints about \(2 \cdot rows - 1\) positions, so runtime is roughly O(rows²).
Variation — User Input Rows
Let the user choose the diamond size at runtime using int.TryParse for safer input handling.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter number of rows: ");
if (!int.TryParse(Console.ReadLine(), out int rows) || rows <= 0)
{
Console.WriteLine("Please enter a positive integer.");
return;
}
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= rows; j++)
Console.Write(i == j ? j.ToString() : " ");
for (int k = rows - 1; k >= 1; k--)
Console.Write(i == k ? k.ToString() : " ");
Console.WriteLine();
}
for (int i = rows - 1; i >= 1; i--)
{
for (int j = 1; j <= rows; j++)
Console.Write(i == j ? j.ToString() : " ");
for (int k = rows - 1; k >= 1; k--)
Console.Write(i == k ? k.ToString() : " ");
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Print a full X by using
i + j == rows + 1in a single rectangle grid - Use fixed-width formatting (e.g.,
Console.Write($"{value,2}")) for 2-digit rows - Replace numbers with characters to create alphabet or star diamonds
- Store each line in a string and print it once for easier formatting changes
Avoid
- Starting the bottom half from
rows(it duplicates the middle row) - Mixing
Console.WriteLineinside inner loops (breaks alignment) - Skipping validation when reading
rowsfrom input
Key Takeaways
Print the top half using i = 1..rows.
Print the bottom half using i = rows-1..1 to avoid duplicates.
The diagonal condition i == j decides when to print a number.
Most of the output is spaces, so runtime scales like O(rows²).
❓ Frequently Asked Questions
rows, the middle row prints twice. Starting from rows-1 prevents duplication.rows x rows grid, print when i == j or i + j == rows + 1.2*rows-1 lines scans ~2*rows-1 positions.Explore More C# Number Patterns!
Practice symmetry patterns to get comfortable with loop boundaries and conditional logic.
Many diamond patterns are built by printing an upper half and then a lower half that mirrors it. Controlling the start and end indices (like rows-1) is the key to avoiding duplicates.
12 people found this page helpful
