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.
Approach
How to Solve It
One fixed-width two-loop idea with if/else or ternary variants.
Method
Idea
Best for
Fixed-width if/else
Left: digit or space; right: space or digit
Learning, interviews, exams
Ternary form
Same logic in one expression per cell
Shorter 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
Goal
Pattern
Grow the filled width
for (i = 1; i <= rows; i++)
Left half
if (j <= i) Console.Write(j); else Console.Write(" ");
Right half
if (k > i) Console.Write(" "); else Console.Write(k);
Ternary left
Console.Write((j <= i) ? j.ToString() : " ");
End the row
Console.WriteLine();
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each digit or space
Console.WriteLine
Ends the current line
After both inner loops
Try it
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 result5 rows · 10 chars/row
1 1
12 21
123 321
1234 4321
1234554321
Trace
Worked Walkthrough — rows = 4
Trace each row: left digits, middle spaces, and the right mirror.
i
Left / gap / right
Printed row
1
1 / 3 spaces / 1
1 1
2
12 / 2+2 spaces / 21
12 21
3
123 / 1+1 spaces / 321
123 321
4
1234 / none / 4321
12344321
Each row is always 2 × rows characters wide — digits and spaces combined.
Code
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();
}
}
}
}
Output
1 1
12 21
123 321
1234 4321
1234554321
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();
}
}
}
}
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.