Diagonal Mirror Number Pyramid in C#

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops + Spacing

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:

Output
    1\n   2 2\n  3   3\n 4     4\n5       5
1

Complete 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.

C#
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

1

Set the number of rows

int rows = 5; controls the height and the maximum number printed.

Setup
2

Outer loop prints each row

for (i = 1; i <= rows; i++) prints one line per value of i.

Row control
3

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.

Left diagonal
4

Second inner loop places the right diagonal

for (k = 2; k <= rows; k++) mirrors the effect and prints again when i == k.

Right diagonal
=

Diagonal mirror pyramid

You check each column position per row, so the time complexity is O(rows²).

2

Variation — User Input Rows

Let the user choose the size of the pattern at runtime.

C#
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 rows to 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

1

You can place values precisely by printing either a number or a space based on a condition.

2

Two inner loops make it easy to print a left half and a mirrored right half.

3

This pattern is a classic nested-loop exercise for spacing and diagonals.

4

For large rows, use fixed-width formatting to keep columns aligned.

❓ Frequently Asked Questions

Because the code places the row number once on the left diagonal and once on the right diagonal, creating a mirrored look.
Starting from 2 avoids duplicating the center position so each row prints the number at most twice.
Use fixed-width printing (for example Console.Write($"{i,2}")) and print matching-width spaces for the empty positions.
O(rows²), because each row prints a full line of positions using nested loops.

Explore More C# Number Patterns!

These diagonal patterns are excellent practice for conditional printing and aligning output with spaces.

All Number Patterns →
Did you know?

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).

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful