C# Hollow Square Number Pattern (Border)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A hollow square border number pattern fills only the boundary of an n×n grid with consecutive numbers clockwise — the interior stays blank, aligned with fixed-width spaces.

Remember
Rule: top j, right k++, bottom l--, left m--  (else three spaces)
      k = n+1,  l = 3n−2,  m = 4(n−1)

  1  2  3  4  5
 16           6
 15           7
 14           8
 13 12 11 10  9     ← n = 5 (numbers 1..16)

Follows the diagonal mirror diamond in Program 58; next is the remove-last-digit pattern in Program 60.

How to Solve It

Visit every cell. Branch by side (top → right → bottom → left). Use three counters for right, bottom, and left; print " " inside.

MethodIdeaBest for
Side-priority ifTop, then right, then bottom, then leftLearning, interviews, exams
General formulask = n+1, l = 3n-2, m = 4(n-1)Any size n >= 2

Pseudocode

Pseudocode
k = n + 1
l = 3 * n - 2
m = 4 * (n - 1)
for i from 1 to n:
    for j from 1 to n:
        if i == 1: print j (width 3)
        else if j == n: print k; k = k + 1
        else if i == n: print l; l = l - 1
        else if j == 1: print m; m = m - 1
        else: print three spaces
    print newline

Cheat sheet

GoalPattern
Top rowif (i == 1) Console.Write("{0,3}", j);
Right columnelse if (j == n) Console.Write("{0,3}", k++);
Bottom rowelse if (i == n) Console.Write("{0,3}", l--);
Left columnelse if (j == 1) Console.Write("{0,3}", m--);
Inner blankelse Console.Write(" ");
Border count4 * (n - 1) for n >= 2

Write vs WriteLine

APIEffectUse for
Console.Write("{0,3}", v) / Write(" ")Stays on the same lineEach cell (number or blank)
Console.WriteLine()Ends the current lineAfter every row of n cells

Print cells with Write (no newline), then call WriteLine() once per row.

Live Preview

Change the square size and the clockwise hollow border updates instantly — capped at 7 so width-3 stays readable.

Whole numbers from 2 to 7. Tap a chip or type a value — the preview redraws as you go.

Live result n = 5 · 16 border cells
  1  2  3  4  5
 16           6
 15           7
 14           8
 13 12 11 10  9

Worked Walkthrough — n = 5

Trace how side priority and the three counters produce numbers 1–16 clockwise.

SideConditionValues
Topi == 11 2 3 4 5
Rightj == 5 (not top)6 7 8 9 via k++
Bottomi == 5 (not right)13 12 11 10 via l-- (+ 9 at corner from k)
Leftj == 1 (middle rows)16 15 14 via m--

Corners follow the first matching branch: top-right is top (5), bottom-right is right (9), bottom-left is bottom (13).

C# Programs

Three complete programs: fixed 5×5, TryParse size, and a compact 3×3 demo. Use View Output to reveal sample results.

Example 1 — Fixed 5×5 Border

Hard-coded size — counters start at 6, 13, and 16 for numbers 1–16.

C#
using System;

class Program
{
    static void Main()
    {
        int k = 6, l = 13, m = 16;

        for (int i = 1; i <= 5; i++)
        {
            for (int 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. Top first. When i == 1, print column index j — including both top corners.

2. Right, bottom, left. Otherwise use k++, l--, or m-- on the matching side.

3. Align. {0,3} and three spaces keep every column three characters wide.

Example 2 — User Input Size

Read n with int.TryParse, set counter starts from formulas, then run the same side logic.

C#
using System;

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

        int k = n + 1;
        int l = 3 * n - 2;
        int m = 4 * (n - 1);

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

How It Works

1. Prompt and validate. Require n >= 2 so a border exists. Prefer TryParse over Parse.

2. Scale counters. k = n+1, l = 3n-2, m = 4(n-1) generalize the 5×5 starts.

3. Safer input tip. Cap demos for readable console output:

Safer input
if (!int.TryParse(Console.ReadLine(), out int n) || n < 2 || n > 9)
{
    Console.WriteLine("Enter a whole number from 2 to 9.");
    return;
}

Example 3 — Compact 3×3 Border

Eight border numbers (1–8) and one inner blank — easy to trace on paper.

C#
using System;

class Program
{
    static void Main()
    {
        int n = 3;
        int k = n + 1, l = 3 * n - 2, m = 4 * (n - 1);

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

How It Works

1. Eight cells. Border count = 4×(3−1) = 8; center is three spaces.

2. Trace on paper. Confirm bottom-right is 5 from k++ (right branch wins over bottom).

3. Scale up next. Once the small demo is clear, use Examples 1–2 for 5×5 or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

branch order

Wrong corner values

Keep the order top → right → bottom → left. Swapping branches changes which counter owns each corner.

{0} only

Crooked columns

Without {0,3}, single-digit and double-digit numbers misalign. Inner blanks must also be three spaces.

WriteLine inside

Broken rows

If WriteLine() sits inside the column loop, each cell lands on its own line. Call it only after the row finishes.

n = 2

No inner cells

Every position is on the border — a good sanity check for the side branches.

wrong l / m

Broken clockwise sequence

Use l = 3n-2 and m = 4(n-1). Hand-tuned starts that ignore n fail when size changes.

TryParse

Validate input

Prefer int.TryParse and require n >= 2 before looping — bad input should not throw.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
Compact 3×3 (Example 3)O(n²)O(1)

Every cell of the n×n grid is visited once → O(n²) time. Only a few counters and loop variables are needed.

Key Takeaways

  • Four sides: top j, right k++, bottom l--, left m--.
  • Branch order matters: corners follow the first matching if.
  • Align with width 3: {0,3} for numbers, " " for inner cells.
  • Complexity: O(n²) time for an n×n visit; O(1) extra space.

One line: walk the grid, print clockwise border numbers with width 3, leave the inside blank.

Frequently Asked Questions

Check sides in order: top (i == 1), right (j == n), bottom (i == n), left (j == 1). Everything else is an inner blank.
Fixed width 3 keeps every column aligned when border numbers have 1 or 2 digits.
An n×n grid where only the boundary shows consecutive numbers clockwise; the inside stays blank.
Program 58 prints a diagonal mirror diamond. Program 59 prints a rectangular hollow border with separate counters per side.
k tracks the right column, l the bottom row (descending), m the left column (descending).
Read n with int.TryParse and set k = n+1, l = 3*n-2, m = 4*(n-1) — see Example 2.
O(n²) for an n×n grid because every cell is visited once.
A 2×2 grid has no inner cells — every position is on the border.

Did you know?

A hollow n×n border has 4(n-1) numbers (for n >= 2). For n = 5 that is 16 cells — top 1..5, right 6..9, bottom 13..9, left 16..13 — with width-3 blanks inside.

Next: Remove Last Digit Pattern

Continue with a shrinking number pattern using integer division by 10.

Program 60 tutorial →

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