Diagonal Mirror Number Diamond in C#

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

What You’ll Learn

How to print a diagonal mirror number diamond in C#. The top half grows from 1 to rows, and the bottom half mirrors back down to 1.

You’ll use the same diagonal-printing logic as Program 57, then add one more loop to generate the lower half.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

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

Complete C# Program

Print the top half from 1 to rows, then mirror it by printing from rows - 1 down to 1.

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

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

Choose the row count

int rows = 5; sets the diamond’s height (it prints 2*rows - 1 lines).

Setup
2

Print the top half

for (i = 1; i <= rows; i++) builds the upper pyramid from 1 to rows.

Top half
3

Place the two diagonals

Two inner loops print either the number or a space. A number is printed only when the column index matches the row index (i == j or i == k).

Diagonals
4

Mirror the bottom half

for (i = rows - 1; i >= 1; i--) reuses the same print logic to generate the lower half.

Bottom half
=

Diagonal mirror diamond

Because you print a grid of positions for each line, runtime grows like O(rows²).

2

Variation — User Input Rows

Read rows from input so you can generate larger or smaller diamonds.

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--)
                    Console.Write(i == j ? j.ToString() : " ");

                for (int k = 2; k <= rows; k++)
                    Console.Write(i == k ? k.ToString() : " ");

                Console.WriteLine();
            }

            for (int i = rows - 1; i >= 1; i--)
            {
                for (int j = rows; j >= 1; j--)
                    Console.Write(i == j ? j.ToString() : " ");

                for (int k = 2; k <= rows; k++)
                    Console.Write(i == k ? k.ToString() : " ");

                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Use a fixed width (e.g., Console.Write($"{val,2}")) when rows go beyond 9
  • Replace numbers with * to create an X-diamond
  • Print a border-only diamond by printing just the diagonal positions (already done here)
  • Swap spaces for dots temporarily to debug alignment

Avoid

  • Mixing tabs and spaces (console alignment will vary)
  • Skipping input validation for rows
  • Printing inconsistent-width values without padding (the diamond will drift)

Key Takeaways

1

The diamond height is \(2 \cdot rows - 1\).

2

Top half counts up; bottom half counts down to mirror the shape.

3

Conditional printing (i == j/i == k) is a simple way to place diagonals.

4

For multi-digit numbers, fixed-width formatting keeps columns aligned.

❓ Frequently Asked Questions

Each row prints the same number on the left diagonal and the right diagonal; spaces fill the remaining positions.
To avoid printing the middle row twice. The top half already printed rows, so we start the mirror at rows - 1.
Yes. Instead of printing only on diagonal positions, print a value for all columns between the two diagonal positions on each row.
O(rows²), because you print a line of positions for each row in both halves.

Explore More C# Number Patterns!

Symmetric patterns like diamonds are great practice for mirroring logic and controlling loop boundaries.

All Number Patterns →
Did you know?

Any diamond-like output can be generated by printing a top half and then repeating the same logic in reverse. This is a common pattern for symmetric console designs.

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