A hollow square border number pattern prints consecutive numbers only on the boundary of an n×n grid — the interior stays blank.
Remember
Rule: numbers on border only (clockwise)
1 2 3 4 5
16 6
15 7
14 8
13 12 11 10 9 ← n = 5
Unlike Program 58 (diagonal diamond), this uses a rectangular grid with side counters — border count = 4(n - 1).
Approach
How to Solve It
Visit every cell; branch by side (i === 1, j === n, i === n, j === 1); pad to width 3.
Method
Idea
Best for
Side counters
Top uses j; right/bottom/left use k, l, m
Classic clockwise demos
Compact
Same logic with n = 3 and scaled starts
Quick dry-runs
Pseudocode
Pseudocode
k = n + 1
l = 3*n - 2
m = 4*(n - 1)
for i from 1 to n:
line = ""
for j from 1 to n:
if i === 1: append padded j
else if j === n: append padded k; k += 1
else if i === n: append padded l; l -= 1
else if j === 1: append padded m; m -= 1
else: append " "
print line
Cheat sheet
Goal
Pattern
Counter starts
k = n+1, l = 3*n-2, m = 4*(n-1)
Top row
if (i === 1) line += String(j).padStart(3, " ");
Right column
else if (j === n) { line += …k; k++; }
Bottom row
else if (i === n) { line += …l; l--; }
Left column
else if (j === 1) { line += …m; m--; }
Interior
else line += " ";
End the row
console.log(line);
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += …
Stays on the same row
Each padded cell
console.log(line)
Ends the current row
After all columns finish
Append without a newline, then end the row once.
Try it
Live Preview
Change the size and the hollow border updates instantly.
Whole numbers from 2 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultn = 5 · 16 border numbers
1 2 3 4 5
16 6
15 7
14 8
13 12 11 10 9
Trace
Worked Walkthrough
Trace the four sides when n = 5 — each side owns its own counter.
Side
Condition
Values
Top
i === 1
1 2 3 4 5
Right
j === 5 (rows 2..5)
6 7 8 9
Bottom
i === 5 (cols 1..4)
13 12 11 10
Left
j === 1 (rows 2..4)
16 15 14
Check sides in order: top → right → bottom → left. The bottom-right corner is claimed by the right branch (j === n).
Code
JavaScript Programs
Three complete programs: fixed 5×5, prompt size with the same clockwise formula, and a compact 3×3 demo. Use View Output for samples, or Try It Yourself to edit and run live.
Example 1 — Fixed 5×5 Border
Hard-coded counters for n = 5: k = 6, l = 13, m = 16.
JavaScript
let k = 6;
let l = 13;
let m = 16;
for (let i = 1; i <= 5; i++) {
let line = "";
for (let j = 1; j <= 5; j++) {
if (i === 1) {
line += String(j).padStart(3, " ");
} else if (j === 5) {
line += String(k).padStart(3, " ");
k++;
} else if (i === 5) {
line += String(l).padStart(3, " ");
l--;
} else if (j === 1) {
line += String(m).padStart(3, " ");
m--;
} else {
line += " ";
}
}
console.log(line);
}
1. Top row uses column index. When i === 1, append padded j for every column.
2. Right, bottom, and left use counters.k++ down the right, l-- across the bottom, m-- up the left.
3. Interior stays blank. Non-border cells append three spaces so columns stay aligned with padStart(3).
Example 2 — User Input Size
Read n with prompt and scale the three counter starts from formulas.
JavaScript
const n = parseInt(prompt("Enter square size (n):"), 10);
if (!Number.isFinite(n) || n < 2) {
console.log("Please enter an integer >= 2.");
} else {
let k = n + 1;
let l = 3 * n - 2;
let m = 4 * (n - 1);
for (let i = 1; i <= n; i++) {
let line = "";
for (let j = 1; j <= n; j++) {
if (i === 1) {
line += String(j).padStart(3, " ");
} else if (j === n) {
line += String(k).padStart(3, " ");
k++;
} else if (i === n) {
line += String(l).padStart(3, " ");
l--;
} else if (j === 1) {
line += String(m).padStart(3, " ");
m--;
} else {
line += " ";
}
}
console.log(line);
}
}
1. Prompt and validate. Require n >= 2 so a hollow border can exist.
2. Scale the starts.k = n+1, l = 3n-2, m = 4(n-1) match Example 1 when n = 5.
3. Entering 4. Twelve border numbers (4×3), last corner is 7 on the bottom-right.
Example 3 — Compact 3×3 Border
Same side-counter logic with n = 3 for a quick visual check.
JavaScript
const n = 3;
let k = n + 1;
let l = 3 * n - 2;
let m = 4 * (n - 1);
for (let i = 1; i <= n; i++) {
let line = "";
for (let j = 1; j <= n; j++) {
if (i === 1) {
line += String(j).padStart(3, " ");
} else if (j === n) {
line += String(k).padStart(3, " ");
k++;
} else if (i === n) {
line += String(l).padStart(3, " ");
l--;
} else if (j === 1) {
line += String(m).padStart(3, " ");
m--;
} else {
line += " ";
}
}
console.log(line);
}