Concentric Number Square in C#

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops + Symmetry

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:

Output
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 5
1

Complete C# Program

Print from the outer number down to the current row value on the left half, then mirror it on the right half.

C#
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

1

Choose the outer value k

k = 5 sets the maximum number and the grid width \(2k-1\).

Setup
2

Outer loop controls the row value

The loop for (i = k; i >= 1; i--) produces row values 5, 4, 3, 2, 1.

Rows
3

Left half prints k..1

For each column, if j > i print j; otherwise print i.

Left
4

Right half mirrors 2..k

The second loop repeats the same rule for j = 2..k to mirror the left side.

Mirror
2

Variation — User Input k

Let the user choose the outer number \(k\). The grid becomes \(2k-1\) wide automatically.

C#
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

1

The pattern is symmetric left-to-right.

2

The center row ends with ... 1 2 3 4 5.

3

Width is \(2k-1\).

4

Time complexity is O(k²).

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