Hollow Square Border Number Pattern in C#

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:
1 2 3 4 5\n16 6\n15 7\n14 8\n13 12 11 10 9Complete 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.
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
Initialize border counters
k, l, and m hold the values used for the right, bottom, and left borders.
Use nested loops for a 5×5 grid
The outer loop (i) controls rows, and the inner loop (j) controls columns.
Print only on the border
Conditions like i == 1, i == 5, j == 1, and j == 5 detect border cells; inner cells print spaces.
Use fixed-width formatting
Console.Write("{0, 3}", value) keeps every column aligned even when values have different digits.
Hollow border square
The program prints a fixed grid of 25 cells, so time complexity is O(n²) for an \(n \times n\) square.
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).
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
A hollow square prints values only when the position is on the border.
Fixed-width formatting keeps the grid aligned.
Multiple counters make it easy to assign different sequences to each side.
This pattern is built with simple border checks and nested loops.
❓ Frequently Asked Questions
i == 1, i == n, j == 1, or j == n.Explore More C# Number Patterns!
Border and grid patterns help you master conditionals, formatting, and loop control.
In many grid patterns, the hardest part isn’t printing numbers—it’s printing the right amount of spacing consistently so everything lines up.
12 people found this page helpful
