Hollow Square Border Numbers in C++

What You’ll Learn
How to print a hollow square border number pattern in C++ using nested loops and conditional logic.
You’ll also see how setw keeps numbers aligned in a grid so the pattern stays readable.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1 2 3 4 5
16 6
15 7
14 8
13 12 11 10 9Complete C++ Program
We print numbers only on the border using if/else. The inside cells print spaces, and setw(3) aligns the columns.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int i, j;
int k = 6, l = 13, m = 16;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= 5; j++) {
if (i == 1)
cout << setw(3) << j;
else if (j == 5)
cout << setw(3) << k++;
else if (i == 5)
cout << setw(3) << l--;
else if (j == 1)
cout << setw(3) << m--;
else
cout << " ";
}
cout << "\n";
}
return 0;
}🧠 How It Works
Loop over a grid
The nested loops go through every cell in a 5×5 grid: i is the row and j is the column.
Top row (1..5)
When i == 1, we print j, producing 1 2 3 4 5.
Right edge, bottom edge, left edge
For the other borders, we print counters (k, l, m) using setw(3) so everything stays aligned.
Inside cells stay blank
If the cell is not on the border, we print three spaces: " ".
Hollow square border
We check each grid position once, so the time complexity is about O(n²) for an \(n \times n\) grid.
Variation — User Input Version
This version reads the size and computes the starting values from rows, so it scales beyond 5×5.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int rows;
cout << "Enter the number of rows (>= 2): ";
cin >> rows;
if (rows < 2) return 0;
int i, j;
int k = rows + 1; // right edge starts after 1..rows
int l = 3 * rows - 2; // bottom-left for rows=5 -> 13
int m = 4 * rows - 4; // left edge top value for rows=5 -> 16
for (i = 1; i <= rows; i++) {
for (j = 1; j <= rows; j++) {
if (i == 1)
cout << setw(3) << j;
else if (j == rows)
cout << setw(3) << k++;
else if (i == rows)
cout << setw(3) << l--;
else if (j == 1)
cout << setw(3) << m--;
else
cout << " ";
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Increase
setwwidth whenrowsis large (multi-digit numbers) - Fill the inside with a different symbol (e.g.,
.) to visualize the grid - Print the numbers in clockwise vs anti-clockwise order by adjusting the border logic
- Create a fully filled square by printing numbers for all cells
- Wrap the border-printing logic into a function for reuse
Avoid
- Mixing different widths/spaces (it will break alignment)
- Forgetting to print spaces for inner cells
- Using
endlinside loops (slower than"\n") - Accepting
rows < 2without handling it
Key Takeaways
Scan a grid with nested loops and print only when you’re on the border.
setw helps keep numbers aligned in columns.
Separate border segments (top/right/bottom/left) using clear conditions.
This conditional-print approach is useful for many hollow shapes.
❓ Frequently Asked Questions
Explore More C++ Number Patterns!
Keep practicing border, hollow, and symmetric patterns to strengthen your loop logic.
Border patterns are a gentle introduction to matrix thinking: every output position is a cell \((i, j)\), and simple conditions decide what appears in that cell.
12 people found this page helpful
