C# Diamond Diagonal Number Pattern

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A mirror diagonal diamond extends Program 53’s V-shape: print the top half from 1 to rows, then mirror the same row logic from rows - 1 back to 1 — total 2n - 1 lines.

Remember
Rule: top    i = 1..rows
      bottom i = rows-1..1
      each row: left i == j, right i == k

1       1
 2     2
  3   3
   4 4
    5
   4 4
  3   3
 2     2
1       1     ← rows = 5

In C# reuse the same row logic twice: top with for (i = 1; i <= rows; i++), bottom with for (i = rows - 1; i >= 1; i--), then WriteLine() after each row.

How to Solve It

Reuse Program 53’s row logic twice — once counting up, once counting down from rows - 1 so the peak row is not duplicated.

MethodIdeaBest for
Two outer loopsTop 1..rows, bottom rows-1..1; same inner diagonalsLearning, interviews, exams
Rows inputSame logic with a user-chosen peak heightPractice / demos

Pseudocode

Pseudocode
for i from 1 to rows:                 // top half
    for j from 1 to rows:
        print i if i == j else a space
    for k from (rows - 1) down to 1:
        print i if i == k else a space
    print newline

for i from (rows - 1) down to 1:      // bottom half
    // same row logic as above
    print newline

Cheat sheet

GoalPattern
Top halffor (i = 1; i <= rows; i++)
Bottom halffor (i = rows - 1; i >= 1; i--)
Left diagonalConsole.Write(i == j ? j.ToString() : " ");
Right diagonalConsole.Write(i == k ? k.ToString() : " "); with k = rows-1..1
Total lines2 * rows - 1

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach column (digit or space) in both halves
Console.WriteLineEnds the current lineAfter both inner loops finish a row

Live Preview

Change the peak row count and the full diamond updates instantly — capped at 9 so every digit stays a single character.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · 9×9 cells
1       1
 2     2 
  3   3  
   4 4   
    5    
   4 4   
  3   3  
 2     2 
1       1

Worked Walkthrough — Peak rows = 5

Count how the two outer loops build nine lines without repeating the tip.

Outer loopi valuesLines printed
Top1, 2, 3, 4, 5V-half ending with tip 5
Bottom4, 3, 2, 1Mirror back — skips i = 5

Total lines = 5 + 4 = 9 = 2n - 1. Each line has 2n - 1 characters → O(n²).

C# Programs

Three complete programs: fixed rows = 5, user-input peak, and a compact rows = 3 demo. Use View Output for sample results.

Example 1 — Fixed rows = 5

Hard-coded peak — top loop 1..rows, bottom loop rows-1..1, same inner diagonal logic each row.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows = 5;
            int i, j, k;

            for (i = 1; i <= rows; i++)
            {
                for (j = 1; j <= rows; j++)
                    Console.Write(i == j ? j.ToString() : " ");

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

                Console.WriteLine();
            }

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

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

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Top half. Same as Program 53 — i runs 1 to 5, printing the growing V.

2. Bottom half. i runs 4 down to 1 with identical inner loops — mirrors without repeating the tip.

3. Result. Nine lines forming a full diamond with digits on both diagonals.

Example 2 — User Input (rows)

Read the peak row count and draw the same full diamond.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows, i, j, k;

            Console.Write("Enter the number of rows: ");
            if (!int.TryParse(Console.ReadLine(), out rows) || rows < 1)
            {
                Console.WriteLine("Please enter a positive whole number.");
                return;
            }

            for (i = 1; i <= rows; i++)
            {
                for (j = 1; j <= rows; j++)
                    Console.Write(i == j ? j.ToString() : " ");

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

                Console.WriteLine();
            }

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

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

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Validate rows. TryParse rejects non-numeric input; require rows >= 1.

2. Same core. Both outer loops and both diagonal conditions adjust automatically from rows.

3. Single-digit tip. Cap demos at rows ≤ 9 so every digit stays one character wide.

Example 3 — Compact rows = 3

Same two-outer-loop structure with a smaller peak for quick paper tracing.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows = 3;
            int i, j, k;

            for (i = 1; i <= rows; i++)
            {
                for (j = 1; j <= rows; j++)
                    Console.Write(i == j ? j.ToString() : " ");

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

                Console.WriteLine();
            }

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

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

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Five lines. Top prints i = 1..3; bottom prints i = 2..1 — five lines total.

2. Trace on paper. Confirm the tip 3 appears once, then the V opens again downward.

Edge Cases & Pitfalls

Check these before calling the solution done.

i = rows twice

Doubled tip

If the bottom loop starts at i = rows, the middle row prints twice. Keep for (i = rows - 1; i >= 1; i--).

missing bottom

V instead of diamond

Omitting the second outer loop leaves only Program 53’s V-half. Add the rows-1..1 pass after the tip.

k = rows

Doubled center column

If the right inner loop starts at k = rows, the middle digit doubles on each row. Keep k = rows - 1.

WriteLine

Broken rows

If WriteLine sits inside either half, each column lands on its own line. Call it only after both inner loops.

rows = 1

Single tip

Output is just 1 — the bottom loop does not run. A good sanity check.

Bad input

Convert.ToInt32 throws

Prefer int.TryParse so non-numeric input does not crash the program.

Time and Space Complexity

ProgramTimeExtra space
Fixed / compact (Examples 1, 3)O(n²)O(1)
User input (Example 2)O(n²)O(1)

2n - 1 lines × 2n - 1 characters each → about 4n² prints. For n = 5 that is 81 characters across 9 lines.

Key Takeaways

  • Two passes: top i = 1..rows, bottom i = rows-1..1 — same i == j / i == k row logic.
  • Skip the tip twice: bottom starts at rows - 1; right half starts at rows - 1.
  • Write vs WriteLine: columns stay on the line; WriteLine advances after both inner loops.
  • Next step: Program 55 fills a triangle column-wise into a 2D array.

One line: top 1..rows, bottom rows-1..1, each row prints diagonal digits with spaces, then WriteLine().

Frequently Asked Questions

The first loop prints the top V-half from i = 1 to rows. The second prints the bottom half from rows-1 down to 1, mirroring the shape.
Starting at rows would print the middle row twice. rows-1 skips the peak row already printed by the top half.
It prints digits on both diagonals per row — top half grows to rows, then the bottom half mirrors back to 1, forming a symmetric diamond.
Program 53 prints only the top V-half. Program 54 adds a second outer loop to mirror the same row logic downward.
The left loop uses i == j for the main diagonal. The right loop uses i == k for the mirrored diagonal, with spaces elsewhere.
Change rows or read it from user input with TryParse — see Example 2.
O(n²) for n rows because the diamond has about 2n-1 lines and each line scans about 2n-1 positions.
Yes. Print when i == j or i + j == rows + 1 in a single column loop.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
One line prints a single 1 — the bottom loop starts at 0 and does not run.

Did you know?

Program 53’s V-shape becomes a full diamond by adding a second outer loop from rows-1 down to 1. Total lines = 2n-1 with about 2n-1 characters per line — O(n²) overall.

Next: Column-Wise Number Triangle

Continue with a triangle filled column-by-column into a 2D array, then printed row-wise.

Program 55 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

6 people found this page helpful