A hollow square border prints a character only on the edges of an n × n grid — first/last row and first/last column — and leaves the interior blank with spaces.
Three complete programs: fixed 5×5, cin size, and an asterisk border. Use View Output to reveal sample results.
Example 1 — Fixed size = 5
Hard-coded grid size — ideal for first demos and screenshots.
C++
#include <iostream>
using namespace std;
int main()
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= 5; j++)
{
if (i == 1 || i == 5 || j == 1 || j == 5)
cout << "1 ";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Output
1 1 1 1 1
1 1
1 1
1 1
1 1 1 1 1
How It Works
1. Nested loops visit every cell. Outer i is the row; inner j is the column.
2. Border condition. If i or j is on an edge, print "1 "; otherwise print " " so columns stay aligned.
3. End the row.cout << "\n" only after the inner loop finishes.
When i = 1 or i = 5, every cell is on the border — a full line of ones.
Example 2 — User Input Size
Read size at runtime and use it in both loops and the border check. Prefer validating cin.
C++
#include <iostream>
using namespace std;
int main()
{
int size;
int i, j;
cout << "Enter square size: ";
cin >> size;
for (i = 1; i <= size; i++)
{
for (j = 1; j <= size; j++)
{
if (i == 1 || i == size || j == 1 || j == size)
cout << "1 ";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Output (when user enters 4)
Enter square size: 4
1 1 1 1
1 1
1 1
1 1 1 1
How It Works
1. Prompt and read. Ask for a size, then store it in size.
2. Same border core. Only the source of size changes — never hard-code 5 in the condition.
3. Safer input tip. Prefer:
Safer input
if (!(cin >> size) || size < 2)
{
cout << "Enter an integer greater than 1.\n";
return 1;
}
Example 3 — Asterisk Border
Keep size = 5 but print * on the border instead of 1.
C++
#include <iostream>
using namespace std;
int main()
{
int size = 5;
int i, j;
for (i = 1; i <= size; i++)
{
for (j = 1; j <= size; j++)
{
if (i == 1 || i == size || j == 1 || j == size)
cout << "* ";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Output
* * * * *
* *
* *
* *
* * * * *
How It Works
1. Same condition. The border test and interior spaces match Example 1 exactly.
2. Only the glyph changes. Swap "1 " for "* " (or "# ") in the if branch.
3. Keep two-character cells. Border and interior both print two characters so columns stay lined up.
Edge Cases & Pitfalls
Check these before calling the solution done.
no if
Solid square
Printing "1 " in every cell fills the grid. You need the border condition for a hollow center.
hard-code 5
Broken NxN
If loops use size but the condition still checks 5, larger grids mis-draw. Use size everywhere.
one space
Misaligned columns
Border uses two characters ("1 "). Interior must use " ", not a single space.
\n early
Column of cells
If cout << "\n" is inside the inner loop, each cell lands on its own line. End the row only after the inner loop.
size = 1
No hollow
Output is just 1 — no interior. Validate size >= 2 for interactive programs.
Bad cin
Validate size
Check cin >> size and require size >= 2 before the loops.
Analysis
Time and Space Complexity
Program
Time
Extra space
Border condition (Examples 1–3)
O(size²)
O(1)
An n × n grid visits n² cells once each. Border cells alone are 4n - 4 for n >= 2, but the loops still scan the whole grid. Only a few integers of extra memory.
Remember
Key Takeaways
Rule: print "1 " when i or j is on an edge; otherwise " ".
Align cells: border and interior both print two characters so columns line up.
Break the row: call cout << "\n" only after the inner loop.
Complexity:O(n²) time for an n × n grid; O(1) extra space.
One line: for each cell (i, j), print "1 " on the border else " ", then end the line after each row.
Frequently Asked Questions
A hollow square prints characters only on the border (first/last row and first/last column) and leaves the inside blank with spaces.
It checks if i is 1 or size, or if j is 1 or size. If any condition is true, it prints 1; otherwise it prints spaces.
Use a size variable and loop from 1 to size for both i and j. Update the border check to use size instead of 5 — see Example 2.
Each border cell uses "1 " (digit plus space). Interior cells use " " so columns stay aligned in the console.
Size 2 gives a thin border frame. Size 1 prints a single 1 with no hollow interior.
Printing a cell stays on the same line. Printing a newline ends the current line. Cells use cout without a newline; the row break uses cout << "\n" after the inner loop.
O(n²) for an n×n grid because each cell is visited once.
After cin >> size, check failure and require size ≥ 2 for a meaningful hollow frame.
🤔
Did you know?
Print 1 when i == 1, i == size, j == 1, or j == size; otherwise print spaces. A size × size grid visits n² cells — total prints = n².