Mirror Diagonal Number Pattern in C#

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

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:

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

Complete C# Program

Print the left diagonal across rows columns, then mirror it across rows-1 columns (so the center column isn’t duplicated).

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

1

Set the number of rows

int rows = 5; decides the height (and the base width) of the pattern.

Setup
2

Loop through each row

for (int i = 1; i <= rows; i++) prints one line per iteration.

Row control
3

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.

Left side
4

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.

Right side
=

Mirror diagonal pattern

Each row prints about \(2 \cdot rows - 1\) characters, so runtime is O(rows²).

2

Variation — User Input Rows

Read rows from the user, validate it, and print the same mirror diagonal pattern.

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 = 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 == j and i + 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 rows columns (it duplicates the center)
  • Mixing Console.WriteLine inside inner loops (breaks alignment)

Key Takeaways

1

Use i == j to print a value on the main diagonal.

2

Mirror the diagonal by printing another half with k = rows-1..1.

3

Most characters printed are spaces, so runtime grows like O(rows²).

4

The same idea extends to X patterns using i + j == rows + 1.

❓ Frequently Asked Questions

Because the mirrored half prints rows-1 positions. That avoids duplicating the center column, so the final line ends with a single rows.
Yes. In a single loop over columns, print a number when i == j or i + j == rows + 1.
Use fixed-width output (e.g., Console.Write($"{value,2}")) and print matching spaces for empty positions.
O(rows²), because for each row you iterate through about 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.

All Number Patterns →
Did you know?

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.

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