Concentric Number Square in C#

What You’ll Learn
How to print a concentric number square where values decrease toward the center and increase back out symmetrically.
We print each row as two parts: left side (descending columns) and right side (ascending columns).
⭐ Pattern Output
For k = 5, the pattern looks like this:
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5Complete C# Program
Print from the outer number down to the current row value on the left half, then mirror it on the right half.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int k = 5;
int i, j;
for (i = k; i >= 1; i--)
{
for (j = k; j >= 1; j--)
{
if (j > i)
Console.Write(j + " ");
else
Console.Write(i + " ");
}
for (j = 2; j <= k; j++)
{
if (j > i)
Console.Write(j + " ");
else
Console.Write(i + " ");
}
Console.WriteLine();
}
}
}
}🧠 How It Works
Choose the outer value k
k = 5 sets the maximum number and the grid width \(2k-1\).
Outer loop controls the row value
The loop for (i = k; i >= 1; i--) produces row values 5, 4, 3, 2, 1.
Left half prints k..1
For each column, if j > i print j; otherwise print i.
Right half mirrors 2..k
The second loop repeats the same rule for j = 2..k to mirror the left side.
Variation — User Input k
Let the user choose the outer number \(k\). The grid becomes \(2k-1\) wide automatically.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int k;
Console.Write("Enter k: ");
if (!int.TryParse(Console.ReadLine(), out k) || k <= 0)
{
Console.WriteLine("Please enter a positive integer.");
return;
}
for (int i = k; i >= 1; i--)
{
for (int j = k; j >= 1; j--)
Console.Write((j > i ? j : i) + " ");
for (int j = 2; j <= k; j++)
Console.Write((j > i ? j : i) + " ");
Console.WriteLine();
}
}
}
}Key Takeaways
The pattern is symmetric left-to-right.
The center row ends with ... 1 2 3 4 5.
Width is \(2k-1\).
Time complexity is O(k²).
12 people found this page helpful
