C# Mirrored Number Pattern (Spaced)

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

What Is This Pattern?

A mirrored number pattern prints 1..i on the left and i..1 on the right, with spaces in between so both halves stay column-aligned until the last row meets.

Remember
Rule: left 1..i + spaces + right i..1

1        1
12      21
123    321
1234  4321
1234554321   ← 5 rows

In C# both inner loops always run rows times. Extra positions print a space so the columns stay fixed. When i equals rows, there are no spaces left and the halves join into one continuous line.

How to Solve It

One fixed-width two-loop idea with if/else or ternary variants.

MethodIdeaBest for
Fixed-width if/elseLeft: digit or space; right: space or digitLearning, interviews, exams
Ternary formSame logic in one expression per cellShorter demos

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to rows:
        if j <= i: print j else print space
    for k from rows down to 1:
        if k > i: print space else print k
    print newline

Cheat sheet

GoalPattern
Grow the filled widthfor (i = 1; i <= rows; i++)
Left halfif (j <= i) Console.Write(j); else Console.Write(" ");
Right halfif (k > i) Console.Write(" "); else Console.Write(k);
Ternary leftConsole.Write((j <= i) ? j.ToString() : " ");
End the rowConsole.WriteLine();

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach digit or space
Console.WriteLineEnds the current lineAfter both inner loops

Live Preview

Change the row count and the mirrored pattern updates instantly — including the character count per row.

Whole numbers from 1 to 9 (keeps digits single-width). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 10 chars/row
1        1
12      21
123    321
1234  4321
1234554321

Worked Walkthrough — rows = 4

Trace each row: left digits, middle spaces, and the right mirror.

iLeft / gap / rightPrinted row
11 / 3 spaces / 11 1
212 / 2+2 spaces / 2112 21
3123 / 1+1 spaces / 321123 321
41234 / none / 432112344321

Each row is always 2 × rows characters wide — digits and spaces combined.

C# Programs

Three complete programs: fixed height, user input with ternaries, and a compact 3-row demo. Use View Output for sample results.

Example 1 — Fixed rows = 5

Two fixed-width loops: left prints 1..i, right prints i..1, with spaces for alignment.

C#
using System;

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

            for (i = 1; i <= 5; i++)
            {
                for (j = 1; j <= 5; j++)
                {
                    if (j <= i)
                        Console.Write(j);
                    else
                        Console.Write(" ");
                }

                for (k = 5; k >= 1; k--)
                {
                    if (k > i)
                        Console.Write(" ");
                    else
                        Console.Write(k);
                }

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Left half. When j <= i, print the digit; otherwise print a space to hold the column.

2. Right half. When k > i, print a space; otherwise print k descending toward 1.

3. Newline. WriteLine() after both loops starts the next wider fill.

Example 2 — User Input

Read the row count and use ternaries for compact digit-or-space logic.

C#
using System;

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

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

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

                Console.WriteLine();
            }
        }
    }
}

How It Works

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

2. Same alignment. Ternaries pick digit vs space; four rows end at 12344321.

Example 3 — Compact rows = 3

A smaller if/else demo — same fixed-width idea, easier to trace by hand.

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++)
                {
                    if (j <= i) Console.Write(j);
                    else Console.Write(" ");
                }

                for (k = rows; k >= 1; k--)
                {
                    if (k > i) Console.Write(" ");
                    else Console.Write(k);
                }

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Same rules. Left fills 1..i; right fills i..1 with spaces for unused cells.

2. Meet in the middle. The last row is 123321 with no gap.

Edge Cases & Pitfalls

Check these before calling the solution done.

skip spaces

Omit the else space branches

Without spaces the right half shifts left every row. Keep Write(" ") for unused cells.

wrong bound

j <= i only as the loop limit

The loop must still run to rows. Use a fixed-width loop and branch inside.

WriteLine early

WriteLine between the two halves

That splits left and right onto separate lines. Call WriteLine() only after both loops finish.

rows = 1

Single 11

Output is 11 — left 1 and right 1 with no spaces.

rows ≤ 0

Empty output

The outer loop never runs. Validate and prompt again for clearer UX.

Bad input

Convert.ToInt32 throws

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

Time and Space Complexity

ProgramTimeExtra space
If/else loops (Examples 1, 3)O(n²)O(1)
Ternary form (Example 2)O(n²)O(1)

There are n rows; each runs two loops of width n, so total work is 2n².

Key Takeaways

  • Rule: left prints 1..i (else space); right prints i..1 (else space).
  • Fixed width: both inner loops always run rows times so columns stay aligned.
  • Write vs WriteLine: digits and spaces stay on the line; WriteLine advances after both halves.
  • Next step: Program 30 prints a right-aligned descending number triangle.

One line: for each row, fill a left 1..i block and a right i..1 block inside fixed-width loops.

Frequently Asked Questions

Spaces keep the left and right halves aligned so the pattern looks symmetric. Without them, the right half shifts left each row.
The right loop runs k from rows down to 1. When k > i it prints a space; otherwise it prints k — building i..1 on the right.
Both inner loops always run rows times. Extra positions are filled with spaces so columns stay aligned.
Program 27 prints a tight palindrome with no alignment spaces. Program 29 uses fixed-width loops and spaces for a symmetric diamond shape.
When i equals rows, both halves fill all columns — 12345 on the left and 54321 on the right meet with no space between.
Replace 5 with rows in both inner loop bounds — see Example 2.
O(n²) for n rows because each row runs two inner loops of width n.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
Yes — Console.Write((j <= i) ? j.ToString() : " ") compacts the if/else logic.

Did you know?

This pattern prints an increasing left half (1..i), then a mirrored right half (i..1). Spaces in the fixed-width loops keep both halves aligned until the final row joins without a gap.

Next: Right-Aligned Descending Triangle

Pad with leading spaces, then print descending digits on each row.

Program 30 tutorial →

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