Hollow Square Border Number Pattern in C#

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

What You’ll Learn

How to print a hollow square border where only the boundary positions contain numbers and the inside stays blank.

This example also shows how to keep columns aligned using Console.Write("{0, 3}", value) for fixed-width formatting.

⭐ Pattern Output

For a 5×5 square, the pattern looks like this:

Output
1  2  3  4  5\n16          6\n15          7\n14          8\n13 12 11 10 9
1

Complete C# Program

The program prints the top row, right column, bottom row, and left column using different counters, and prints spaces for the inner cells.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j;
            int k = 6, l = 13, m = 16;

            for (i = 1; i <= 5; i++)
            {
                for (j = 1; j <= 5; j++)
                {
                    if (i == 1)
                        Console.Write("{0, 3}", j);
                    else if (j == 5)
                        Console.Write("{0, 3}", k++);
                    else if (i == 5)
                        Console.Write("{0, 3}", l--);
                    else if (j == 1)
                        Console.Write("{0, 3}", m--);
                    else
                        Console.Write("   ");
                }
                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Initialize border counters

k, l, and m hold the values used for the right, bottom, and left borders.

Setup
2

Use nested loops for a 5×5 grid

The outer loop (i) controls rows, and the inner loop (j) controls columns.

Grid
3

Print only on the border

Conditions like i == 1, i == 5, j == 1, and j == 5 detect border cells; inner cells print spaces.

Conditionals
4

Use fixed-width formatting

Console.Write("{0, 3}", value) keeps every column aligned even when values have different digits.

Formatting
=

Hollow border square

The program prints a fixed grid of 25 cells, so time complexity is O(n²) for an \(n \times n\) square.

2

Variation — Make It Configurable

Read the square size from the user. This example keeps the same border-only idea and uses fixed-width formatting (the numbering rule can be customized).

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter square size (n): ");
            if (!int.TryParse(Console.ReadLine(), out int n) || n < 2)
            {
                Console.WriteLine("Please enter an integer >= 2.");
                return;
            }

            // Simple border demo: print row/col indices on the border.
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    bool isBorder = i == 1 || i == n || j == 1 || j == n;
                    Console.Write(isBorder ? string.Format("{0,3}", j) : "   ");
                }
                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Use a larger width (like 4) if your numbers can exceed 99
  • Replace inner spaces with dots to visualize alignment while debugging
  • Change the border numbering rule (clockwise, anti-clockwise, or per-side sequences)
  • Fill the square by printing values for the inner cells too

Avoid

  • Mixing tabs and spaces (your grid may not align consistently)
  • Printing variable-width values without formatting (alignment breaks quickly)
  • Forgetting newline after each row

Key Takeaways

1

A hollow square prints values only when the position is on the border.

2

Fixed-width formatting keeps the grid aligned.

3

Multiple counters make it easy to assign different sequences to each side.

4

This pattern is built with simple border checks and nested loops.

❓ Frequently Asked Questions

A cell is on the border if i == 1, i == n, j == 1, or j == n.
Because each border number is printed in a width of 3, the inner cells also use 3 spaces to maintain alignment.
Yes. Replace the inner-cell branch that prints spaces with a value you want to show inside the square.
O(n²) for an \(n \times n\) grid.

Explore More C# Number Patterns!

Border and grid patterns help you master conditionals, formatting, and loop control.

All Number Patterns →
Did you know?

In many grid patterns, the hardest part isn’t printing numbers—it’s printing the right amount of spacing consistently so everything lines up.

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