Mirror Diagonal Diamond Pattern in C#

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

What You’ll Learn

How to print a mirror diagonal diamond number pattern in C# by printing the top half first and then mirroring it to form the bottom half.

This program is a practical way to learn nested loops, conditional printing, and symmetry in console patterns.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

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

Complete C# Program

First print the top half (1..rows), then print the bottom half (rows-1..1) to mirror the shape.

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();
            }

            for (int i = rows - 1; i >= 1; 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

Choose the number of rows

int rows = 5; sets the diamond height (which becomes 2*rows - 1 lines).

Setup
2

Print the top half

for (i = 1; i <= rows; i++) prints the upper part from 1 to rows.

Top
3

Print the left + mirrored diagonals

The two inner loops print numbers only when indices match (i == j and i == k). Otherwise, they print spaces to keep alignment.

Diagonal logic
4

Mirror the bottom half

for (i = rows - 1; i >= 1; i--) reprints the same diagonal rows in reverse order, which forms the diamond.

Bottom
=

Diamond via symmetry

There are \(2 \cdot rows - 1\) lines, and each line prints about \(2 \cdot rows - 1\) positions, so runtime is roughly O(rows²).

2

Variation — User Input Rows

Let the user choose the diamond size at runtime using int.TryParse for safer input handling.

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();
            }

            for (int i = rows - 1; i >= 1; 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

  • Print a full X by using i + j == rows + 1 in a single rectangle grid
  • Use fixed-width formatting (e.g., Console.Write($"{value,2}")) for 2-digit rows
  • Replace numbers with characters to create alphabet or star diamonds
  • Store each line in a string and print it once for easier formatting changes

Avoid

  • Starting the bottom half from rows (it duplicates the middle row)
  • Mixing Console.WriteLine inside inner loops (breaks alignment)
  • Skipping validation when reading rows from input

Key Takeaways

1

Print the top half using i = 1..rows.

2

Print the bottom half using i = rows-1..1 to avoid duplicates.

3

The diagonal condition i == j decides when to print a number.

4

Most of the output is spaces, so runtime scales like O(rows²).

❓ Frequently Asked Questions

The first outer loop prints the top half of the diamond. The second outer loop prints the same rows in reverse order to create the bottom half.
If you start from rows, the middle row prints twice. Starting from rows-1 prevents duplication.
Yes. In an rows x rows grid, print when i == j or i + j == rows + 1.
O(rows²), because each of the ~2*rows-1 lines scans ~2*rows-1 positions.

Explore More C# Number Patterns!

Practice symmetry patterns to get comfortable with loop boundaries and conditional logic.

All Number Patterns →
Did you know?

Many diamond patterns are built by printing an upper half and then a lower half that mirrors it. Controlling the start and end indices (like rows-1) is the key to avoiding duplicates.

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