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.
Approach
How to Solve It
Visit every cell. Branch by side (top → right → bottom → left). Use three counters for right, bottom, and left; print " " inside.
Method
Idea
Best for
Side-priority if
Top, then right, then bottom, then left
Learning, interviews, exams
General formulas
k = 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
Goal
Pattern
Top row
if (i == 1) Console.Write("{0,3}", j);
Right column
else if (j == n) Console.Write("{0,3}", k++);
Bottom row
else if (i == n) Console.Write("{0,3}", l--);
Left column
else if (j == 1) Console.Write("{0,3}", m--);
Inner blank
else Console.Write(" ");
Border count
4 * (n - 1) for n >= 2
Write vs WriteLine
API
Effect
Use for
Console.Write("{0,3}", v) / Write(" ")
Stays on the same line
Each cell (number or blank)
Console.WriteLine()
Ends the current line
After every row of n cells
Print cells with Write (no newline), then call WriteLine() once per row.
Try it
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 resultn = 5 · 16 border cells
1 2 3 4 5
16 6
15 7
14 8
13 12 11 10 9
Trace
Worked Walkthrough — n = 5
Trace how side priority and the three counters produce numbers 1–16 clockwise.
Side
Condition
Values
Top
i == 1
1 2 3 4 5
Right
j == 5 (not top)
6 7 8 9 via k++
Bottom
i == 5 (not right)
13 12 11 10 via l-- (+ 9 at corner from k)
Left
j == 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).
Code
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();
}
}
}
Output
1 2 3 4 5
16 6
15 7
14 8
13 12 11 10 9
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();
}
}
}
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();
}
}
}
Output
1 2 3
8 4
7 6 5
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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.