Diagonal Mirror Number Pyramid in C#

What You’ll Learn
How to print a diagonal mirror number pyramid in C# where the row number appears on both diagonals. For 5 rows, you’ll print 1, then 2 2, then 3 3, and so on.
You’ll do this by using nested loops and printing either a number or a space depending on the current column position.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1\n 2 2\n 3 3\n 4 4\n5 5Complete C# Program
This approach uses two inner loops. Each prints the row number only when the loop index matches the current row; otherwise it prints a space.
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();
}
}
}
}🧠 How It Works
Set the number of rows
int rows = 5; controls the height and the maximum number printed.
Outer loop prints each row
for (i = 1; i <= rows; i++) prints one line per value of i.
First inner loop places the left diagonal
for (j = rows; j >= 1; j--) prints the number only when i == j; otherwise it prints a space.
Second inner loop places the right diagonal
for (k = 2; k <= rows; k++) mirrors the effect and prints again when i == k.
Diagonal mirror pyramid
You check each column position per row, so the time complexity is O(rows²).
Variation — User Input Rows
Let the user choose the size of the pattern at runtime.
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--)
{
if (i == j) Console.Write(j);
else Console.Write(" ");
}
for (int k = 2; k <= rows; k++)
{
if (i == k) Console.Write(k);
else Console.Write(" ");
}
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Replace spaces with a dot (
.) temporarily to debug alignment - Print a character (like
*) instead of numbers for a clean X-shape - Increase
rowsto generate a taller pyramid - Use
Console.Write($"{i} ")and fixed-width formatting for multi-digit rows
Avoid
- Mixing tabs and spaces (alignment can change per console)
- Forgetting to validate user input when reading
rows - Printing extra spaces at the end if you are comparing exact outputs
Key Takeaways
You can place values precisely by printing either a number or a space based on a condition.
Two inner loops make it easy to print a left half and a mirrored right half.
This pattern is a classic nested-loop exercise for spacing and diagonals.
For large rows, use fixed-width formatting to keep columns aligned.
❓ Frequently Asked Questions
2 avoids duplicating the center position so each row prints the number at most twice.Console.Write($"{i,2}")) and print matching-width spaces for the empty positions.Explore More C# Number Patterns!
These diagonal patterns are excellent practice for conditional printing and aligning output with spaces.
Many patterns are just coordinates in disguise. Here, the two diagonals happen when a column index equals the row index in one direction (and equals it again in the mirrored direction).
12 people found this page helpful
