Mirrored Number Pattern in C#

What You’ll Learn
How to print a mirrored number pattern where the left side increases (1..i) and the right side decreases (i..1). Spaces in the middle keep the mirror aligned.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
Output
1 1
12 21
123 321
1234 4321
12345543211
Complete C# Program
This follows the reference approach exactly: one loop prints the left half (or spaces), and another loop prints the right half (or spaces).
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();
}
}
}
}2
Variation — User Input rows
Same idea, but make the height configurable:
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter rows: ");
int rows = Convert.ToInt32(Console.ReadLine());
if (rows < 1) return;
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= rows; j++)
Console.Write((j <= i) ? j.ToString() : " ");
for (int k = rows; k >= 1; k--)
Console.Write((k > i) ? " " : k.ToString());
Console.WriteLine();
}
}
}
}12 people found this page helpful
