C# Diagonal Mirror Number Pattern

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

What Is This Pattern?

A mirror diagonal pattern prints digit i on the main diagonal and again on a mirrored diagonal. Spaces fill every other column, forming a V-shape that meets at the bottom tip.

Remember
Rule: left  j = 1..rows   → print i if i == j else space
      right k = rows-1..1 → print i if i == k else space

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

In C# use two inner loops: left half with Console.Write(i == j ? j.ToString() : " "), right half with i == k while k counts down from rows - 1, then WriteLine().

How to Solve It

Use one outer loop and two inner loops. Left half tests i == j; right half tests i == k while counting k down from rows - 1 (skip the center column).

MethodIdeaBest for
Two halves + conditionsLeft i == j, right i == k, else spaceLearning, interviews, exams
Rows inputSame logic with a user-chosen heightPractice / demos

Pseudocode

Pseudocode
for i from 1 to rows:
    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

Cheat sheet

GoalPattern
Pick each rowfor (i = 1; i <= rows; i++)
Left diagonalfor (j = 1; j <= rows; j++) Console.Write(i == j ? j.ToString() : " ");
Right diagonalfor (k = rows - 1; k >= 1; k--) Console.Write(i == k ? k.ToString() : " ");
Skip center twiceRight loop starts at rows - 1, not rows
Chars per row2 * 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 row count and the V-shape 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 · 5×9 cells
1       1
 2     2 
  3   3  
   4 4   
    5    

Worked Walkthrough — Row i = 3, rows = 5

Trace where digit 3 lands on both diagonals.

HalfLoopWhen digit prints
Leftj = 1..5At j = 3 → 3 (spaces elsewhere)
Rightk = 4..1At k = 3 → 3 (spaces elsewhere)

Full row: 3 3 (9 characters). Each of n rows prints 2n - 1 chars → O(n²).

C# Programs

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

Example 1 — Fixed rows = 5

Hard-coded height — print digit when i == j or i == k, otherwise a space.

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

How It Works

1. Left half. Scan j = 1..rows; print the digit only when i == j (main diagonal).

2. Right half. Scan k = rows-1..1; print when i == k — starting at rows - 1 skips the center column.

3. Bottom tip. On the last row, both diagonals meet — only one digit appears in the middle.

Example 2 — User Input (rows)

Read the row count and draw the same V-shaped mirror diagonals.

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

How It Works

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

2. Same core. Only the loop limits change — both diagonal conditions stay identical.

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

Example 3 — Compact rows = 3

Same two-loop structure with a smaller height 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();
            }
        }
    }
}

How It Works

1. Three rows. Wide V on row 1, closer digits on row 2, single tip on row 3.

2. Trace on paper. Confirm the right loop starts at 2 (not 3) so the center is not doubled.

Edge Cases & Pitfalls

Check these before calling the solution done.

k = rows

Doubled center

If the right loop starts at k = rows, the middle column prints twice. Keep k = rows - 1.

print i always

No spaces

Printing i on every column fills the row with digits. Use the ternary: digit only when i == j / i == k.

WriteLine

Broken rows

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

rows = 1

Single tip

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

rows > 9

Multi-digit values

Past 9, values like 10 break column alignment. Cap demos at 9 or use fixed-width formatting.

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)

Each of n rows prints 2n - 1 characters → about 2n² prints. For n = 5 that is 45 characters.

Key Takeaways

  • Two diagonals: left uses i == j, right uses i == k, else space.
  • Skip the center twice: right loop starts at rows - 1 so the middle column is not doubled.
  • Write vs WriteLine: columns stay on the line; WriteLine advances after both halves.
  • Next step: Program 54 mirrors this V downward into a full diamond.

One line: for each i, print digit on i == j and i == k, spaces elsewhere, then WriteLine().

Frequently Asked Questions

The mirrored half prints rows-1 positions to avoid duplicating the center column. On the final row, only the main diagonal digit remains.
It prints the row number on the main diagonal (left) and on a mirrored diagonal (right), creating a symmetric V-shape like 1..5 on both sides.
The first loop prints the left half across rows columns. The second prints the right mirrored half across rows-1 columns in reverse.
Skipping the center column prevents printing the middle digit twice on rows where i equals the center index.
Change rows or read it from user input with TryParse — see Example 2.
O(n²) for n rows because each row prints about 2n-1 characters using nested loops.
Program 52 builds palindromic digit rows with m++ and m--. Program 53 uses spacing and i == j / i == k to place digits on mirrored diagonals.
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 row prints a single 1 — the left loop prints at j = 1 and the right loop does not run.

Did you know?

Each row prints the row number on the main diagonal (left) and on a mirrored diagonal (right) using i == j and i == k. Row 3 shows 3 on both sides — about 2n-1 characters per row, so O(n²) total.

Next: Mirror Diagonal Diamond

Continue with the full diamond — V on top plus an inverted V below.

Program 54 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