Shape Rule
Border only
Row 1 and row 5 are all 1s; middle rows have 1 at both ends and spaces between.

The hollow square border prints 1s on the edges and spaces inside a 5 × 5 grid — a natural step after Program 41’s square-number pyramid. This tutorial covers nested loops with border conditions, a live preview, worked C# examples, edge cases, and complexity.
Border only
Row 1 and row 5 are all 1s; middle rows have 1 at both ends and spaces between.
i = 1..size
for (i = 1; i <= size; i++) walks each row of the grid.
j = 1..size
for (j = 1; j <= size; j++) walks each column within the current row.
i or j on edge
i == 1 || i == size || j == 1 || j == size prints 1; else print spaces.
3–9 size
Pick a square size and draw the hollow border in the browser.
Complexity
Each cell visited once — n² prints for n × n; extra memory stays O(1).
A hollow square border prints 1 only on the first/last row and first/last column; interior cells are blank spaces. With size = 5, the output is a 5 × 5 frame of 1s with a hollow center.
In C# the outer loop runs i = 1..size, the inner loop runs j = 1..size, and a border condition picks "1 " or " " per cell.
It teaches grid coordinates and boundary checks — a key step after Program 41’s formatted pyramid.
First/last row or column.
Interior prints " ".
Program 41 prints squares in a pyramid; Program 42 prints a hollow grid.
Follow Program 41; continue to Program 43 next.
In short: nested loops over i, j, border check prints "1 ", else " ", then WriteLine().
Given a square size (e.g. 5), print a hollow border of 1s — edges filled, interior blank.
// size = 5
//1 1 1 1 1
//1 1
//1 1
//1 1
//1 1 1 1 1 | Item | Type | Description |
|---|---|---|
size | int | Side length of the square grid (e.g. 5 for 5×5). |
i | int | Outer loop — current row index (1 to size). |
j | int | Inner loop — current column index (1 to size). |
for i from 1 to size:
for j from 1 to size:
if i or j is on border:
print "1 "
else:
print " "
print newline | Approach | Idea | Best for |
|---|---|---|
| Nested loops + condition | 1 1 1 1 1, hollow center | Learning and interviews |
| User-input size | int.TryParse(...) | Flexible N×N grids |
| Custom border char | "* " instead of "1 " | Alternate border symbol |
| Goal | Pattern |
|---|---|
| Walk rows | for (i = 1; i <= size; i++) |
| Walk columns | for (j = 1; j <= size; j++) |
| Border check | if (i == 1 || i == size || j == 1 || j == size) |
| Print border | Console.Write("1 "); |
| Print interior | Console.Write(" "); |
| Program 41 contrast | Square-number pyramid with m*m — not a hollow grid |
Same hollow square — different ways to control size and border character.
i = 1..sizeRows of the grid
j = 1..sizeColumns per row
i/j on edgeCondition per cell
"1 " / " "Two chars per cell
Reach for this pattern when teaching 2D grids, boundary conditions, and hollow shapes.
Natural follow-up after Program 41 — same nested loops but adds per-cell border logic.
Outer/inner bound practice with an immediate visual check.
Combine loops with ReadLine for a flexible row count.
Compare Program 41 (square pyramid) and Program 43 (next in series) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a square size between 3 and 9 and draw the hollow border in the browser.
Three complete C# programs — fixed rows, user input, and a smaller trace demo. Click View Output to reveal sample console results.
Print a 5×5 hollow square border with nested loops and a border condition.
size = 5Hard-coded grid size — ideal for first demos and screenshots.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= 5; j++)
{
if (i == 1 || i == 5 || j == 1 || j == 5)
Console.Write("1 ");
else
Console.Write(" ");
}
Console.WriteLine();
}
}
}
} When i = 1 or i = 5, every cell is on the border — all 1s. When i = 3 and j = 3, neither row nor column is on the edge — prints spaces.
Read the square size from the console instead of hard-coding 5.
Read size from the console with safe parsing.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int size;
Console.Write("Enter square size: ");
if (!int.TryParse(Console.ReadLine(), out size) || size <= 1)
{
Console.WriteLine("Please enter an integer greater than 1.");
return;
}
for (int i = 1; i <= size; i++)
{
for (int j = 1; j <= size; j++)
{
if (i == 1 || i == size || j == 1 || j == size)
Console.Write("1 ");
else
Console.Write(" ");
}
Console.WriteLine();
}
}
}
} Same inner-loop core as Example 1; only the source of rows changes from a literal to user input.
Swap 1 for * on the border — same condition, different character.
Keep size = 5 but print * on the border instead of 1.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int size = 5;
for (int i = 1; i <= size; i++)
{
for (int j = 1; j <= size; j++)
{
if (i == 1 || i == size || j == 1 || j == size)
Console.Write("* ");
else
Console.Write(" ");
}
Console.WriteLine();
}
}
}
} Only the border character changes — "* " instead of "1 ". The border condition and interior spaces stay the same as Example 1.
using System; brings in Console. Set loop variables i, j for a 5 × 5 grid.
for (i = 1; i <= size; i++) and for (j = 1; j <= size; j++) visit every cell in the grid.
if (i == 1 || i == size || j == 1 || j == size) — true on any edge cell.
Border cells print "1 "; interior cells print " " to keep columns aligned.
Console.WriteLine() ends each row after the inner loop finishes.
Every cell visited once — O(n²) time for n × n, O(1) extra memory.
size = 5, row i = 3Trace each column j on row 3 — which cells are border vs interior.
j | On border? | Prints |
|---|---|---|
1 | Yes (j == 1) | 1 |
2 | No | |
3 | No | |
4 | No | |
5 | Yes (j == 5) | 1 |
Row 3 output: 1 1 — border cells at both ends, spaces in between. Total cells = size² = 25 for a 5×5 grid.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change border to * or # — see Example 3.
Foundation for rectangles, diamonds, frames, and filled variants.
Example: continue to Program 43 for the next pattern in the series.
Practice Write vs WriteLine without complex math.
Example: put WriteLine inside the inner loop by mistake.
Add a different border character once the two-loop structure works.
Example: use "* " instead of "1 " on the border.
Triangular totals make O(n²) concrete for beginners.
Example: count border cells for size = 5 — total grid cells = 25, border = 16.
Pair the pattern with TryParse and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C# courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Change border char, fill interior with 0, or scale to rectangles with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace i = 3 and each j on paper before coding — watch how corner cells match two border conditions.
Small habits that keep number-pattern code clean.
Never hard-code 5 in the border check — use size everywhere.
TryParseAvoid crashes when the user types letters instead of a number.
Only call WriteLine() after the inner loop finishes the row.
Border uses "1 " (2 chars); interior must use " " (2 spaces) for alignment.
Trace i = 1, 2, 3 and each j on paper before coding the full size = 5 demo.
Pro Tip: if the output is a vertical list of single squares per line, you almost certainly put WriteLine inside the print loop.
Mistakes that commonly break hollow square border patterns.
Each cell lands on its own line — you get a column, not a square.
→ Use Write("1 ") or Write(" ") per cell; WriteLine only after the inner loop.
Using i == 5 in the check breaks when size changes to 7 or 10.
→ Always use size variable: i == size || j == size.
Border prints "1 " but interior prints a single space — columns drift apart.
→ Use two spaces for interior: Console.Write(" ") to match "1 " width.
size = 1 prints a single 1 with no hollow interior; size = 2 is the thinnest frame.
→ Validate size >= 2 for interactive programs expecting a hollow square.
Letters or empty input throw FormatException.
→ Prefer int.TryParse and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line — no hollow interior.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Thinnest hollow frame — four border cells forming a square ring.
Convert.ToInt32 throws — use TryParse.
Each cell visited once — total work grows as n² for n × n grid.
Try these variations to lock in the pattern.
m*m pyramid1 in every cell — no border checkelse branch"* " instead of "1 "1 when i == 1 || i == size || j == 1 || j == size; else print two spaces.Console.Write stays on the line; WriteLine advances — mix them carefully.rows > 0 for interactive programs; rows = 1 should print a single 1.size × size grid has size² cells — border cells = 4*size - 4 for size >= 2.Quick Takeaway: nested loops over i, j, border check prints "1 ", else " ", then WriteLine().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The hollow square border is a compact nested-loop lesson: visit every cell in an n × n grid and use a border condition to print 1 or spaces. Master the fixed-size version, then try user input and a custom border character.
Practice the three examples above, then continue to Program 43 for the next pattern in the series.
Border = first/last row or column — keep cell width consistent ("1 " vs " ") and validate size when reading input.
for (i = 1; i <= size; i++) and for (j = 1; j <= size; j++)if (i == 1 || i == size || j == 1 || j == size)"1 " on border, " " insidesize ≥ 2 for interactive programsint.TryParse over bare Convert.ToInt32WriteLine inside the inner cell loop5 in the border conditionrows = 1 edge casePrint the pattern the beginner-friendly way.
Border cells only
DefinitionRows i = 1..size
CodeColumns j = 1..size
Codei/j on edge
LogicO(n²) time
AnalysisPrint 1 when i == 1, i == size, j == 1, or j == size; otherwise print spaces. A size × size grid visits n² cells — total prints = n².
Move on to the next pattern in the C# number-pattern series.
12 people found this page helpful