Mirror Diagonal Number Pattern in C#

What You’ll Learn
How to print a mirror diagonal number pattern in C# where each row shows the same number on the left diagonal and the right mirrored diagonal.
You’ll use nested loops and a simple condition (i == j) to decide when to print a digit and when to print a space.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1 1\n 2 2\n 3 3\n 4 4\n 5Complete C# Program
Print the left diagonal across rows columns, then mirror it across rows-1 columns (so the center column isn’t duplicated).
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();
}
}
}
}🧠 How It Works
Set the number of rows
int rows = 5; decides the height (and the base width) of the pattern.
Loop through each row
for (int i = 1; i <= rows; i++) prints one line per iteration.
Print the left diagonal
The first inner loop runs j = 1..rows. Only when i == j, it prints the number; otherwise it prints a space.
Mirror on the right
The second inner loop runs k = rows-1..1 so the middle column is not repeated. When i == k, it prints k.
Mirror diagonal pattern
Each row prints about \(2 \cdot rows - 1\) characters, so runtime is O(rows²).
Variation — User Input Rows
Read rows from the user, validate it, and print the same mirror diagonal pattern.
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();
}
}
}
}💡 Tips for Enhancement
Try These
- Replace numbers with
*to create an X-like star pattern - Print a full “X” by mirroring across both diagonals (use
i == jandi + j == rows + 1) - Use fixed-width formatting when rows can be 10+ (alignment with two-digit values)
- Use a character matrix and print it after filling, for easier debugging
Avoid
- Forgetting to validate user input (rows must be positive)
- Printing the mirrored half with
rowscolumns (it duplicates the center) - Mixing
Console.WriteLineinside inner loops (breaks alignment)
Key Takeaways
Use i == j to print a value on the main diagonal.
Mirror the diagonal by printing another half with k = rows-1..1.
Most characters printed are spaces, so runtime grows like O(rows²).
The same idea extends to X patterns using i + j == rows + 1.
❓ Frequently Asked Questions
rows-1 positions. That avoids duplicating the center column, so the final line ends with a single rows.i == j or i + j == rows + 1.Console.Write($"{value,2}")) and print matching spaces for empty positions.2*rows - 1 positions using nested loops.Explore More C# Number Patterns!
Diagonal and symmetric patterns are great practice for conditions, spacing, and loop boundaries.
A common shortcut for X/diagonal patterns is using index math: the main diagonal uses i == j, and the anti-diagonal uses i + j == rows + 1.
12 people found this page helpful
