Diagonal Mirror Number Diamond in C#

What You’ll Learn
How to print a diagonal mirror number diamond in C#. The top half grows from 1 to rows, and the bottom half mirrors back down to 1.
You’ll use the same diagonal-printing logic as Program 57, then add one more loop to generate the lower half.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1\n 2 2\n 3 3\n 4 4\n 5 5\n 4 4\n 3 3\n 2 2\n 1Complete C# Program
Print the top half from 1 to rows, then mirror it by printing from rows - 1 down to 1.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j, k;
int rows = 5;
for (i = 1; i <= rows; i++)
{
for (j = rows; j >= 1; j--)
{
if (i == j)
Console.Write(j);
else
Console.Write(" ");
}
for (k = 2; k <= rows; k++)
{
if (i == k)
Console.Write(k);
else
Console.Write(" ");
}
Console.WriteLine();
}
for (i = rows - 1; i >= 1; i--)
{
for (j = rows; j >= 1; j--)
{
if (i == j)
Console.Write(j);
else
Console.Write(" ");
}
for (k = 2; k <= rows; k++)
{
if (i == k)
Console.Write(k);
else
Console.Write(" ");
}
Console.WriteLine();
}
}
}
}🧠 How It Works
Choose the row count
int rows = 5; sets the diamond’s height (it prints 2*rows - 1 lines).
Print the top half
for (i = 1; i <= rows; i++) builds the upper pyramid from 1 to rows.
Place the two diagonals
Two inner loops print either the number or a space. A number is printed only when the column index matches the row index (i == j or i == k).
Mirror the bottom half
for (i = rows - 1; i >= 1; i--) reuses the same print logic to generate the lower half.
Diagonal mirror diamond
Because you print a grid of positions for each line, runtime grows like O(rows²).
Variation — User Input Rows
Read rows from input so you can generate larger or smaller diamonds.
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 = rows; j >= 1; j--)
Console.Write(i == j ? j.ToString() : " ");
for (int k = 2; k <= rows; k++)
Console.Write(i == k ? k.ToString() : " ");
Console.WriteLine();
}
for (int i = rows - 1; i >= 1; i--)
{
for (int j = rows; j >= 1; j--)
Console.Write(i == j ? j.ToString() : " ");
for (int k = 2; k <= rows; k++)
Console.Write(i == k ? k.ToString() : " ");
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Use a fixed width (e.g.,
Console.Write($"{val,2}")) when rows go beyond 9 - Replace numbers with
*to create an X-diamond - Print a border-only diamond by printing just the diagonal positions (already done here)
- Swap spaces for dots temporarily to debug alignment
Avoid
- Mixing tabs and spaces (console alignment will vary)
- Skipping input validation for
rows - Printing inconsistent-width values without padding (the diamond will drift)
Key Takeaways
The diamond height is \(2 \cdot rows - 1\).
Top half counts up; bottom half counts down to mirror the shape.
Conditional printing (i == j/i == k) is a simple way to place diagonals.
For multi-digit numbers, fixed-width formatting keeps columns aligned.
❓ Frequently Asked Questions
rows, so we start the mirror at rows - 1.Explore More C# Number Patterns!
Symmetric patterns like diamonds are great practice for mirroring logic and controlling loop boundaries.
Any diamond-like output can be generated by printing a top half and then repeating the same logic in reverse. This is a common pattern for symmetric console designs.
12 people found this page helpful
