When learning C++ loops, pattern programs are a good way to understand
how loops work. the hollow square pattern is especially useful
because it teaches two things at the same time: nested loops and
conditions.
In a solid square, every position contains a star. in a hollow square,
only the outside border contains stars. the middle part is empty.
For example, if the size is 5, we get:
* * * * * * * * * * * * * * * *
This may look a little harder than a solid square, but the logic is
actually simple. we just need to find out whether the current position is
on the border or inside the square.
Pattern Output:
Suppose the user enters:
5
The output will be:
* * * * * * * * * * * * * * * *
Let's look at this pattern carefully.
* * * * * ← First row * * ← Only first and last stars * * ← Only first and last stars * * ← Only first and last stars * * * * * ← Last row
The first row is completely filled.
The last row is also completely filled
In the middle rows, only the first and last positions contain
stars.
Everything between them is empty. That is why it is called a
hollow square.
Pattern Logic:
The easiest way to understand this pattern is to imagine a square with rows
and columns.
For n = 5, there are:
5 rows
5 columns
We can think of every position like this:
Column
1 2 3 4 5
Row 1 * * * * *
Row 2 * *
Row 3 * *
Row 4 * *
Row 5 * * * * *
Now ask one simple question for every position:
Is this position on the border of the square?
If the answer is yes, print * .
If the answer is no, print spaces.
A position is on the border when it is :
In the first row
In the last row
In the first column
In the last column
In the program:
i represents the row
j represents the column
n is the size of the square
So the border condition is:
i == 1 || i == n || j == 1 || j == n
Let's understand it one part at a time.
First row
i == 1
When i is 1, we are printing the first row.
So every position in that row gets a star.
* * * * *
Last row
i == n
When i reaches n5 , we are printing the last row.
Again, every position gets a star.
* * * * *
First column
j == 1
This prints the star at the beginning of every middle row.
*
Last column
j == n
This prints the star at the end of every middle row.
*
When all these conditions are combined, we get the complete border.
Algorithm :
1) Start the program.
2) Declare a variable n to store the size of the square.
3) Read the value of n.
4) Use an outer loop to print n rows.
5) For every row, use an inner loop to check n positions.
6) Check whether the current position is:
- The first row
- The last row
- The first column or the last column.
7) If it is a border position, print * .
8) Otherwise, print spaces.
9) After finishing one row, move to the next line.
10) Repeat until all rows are printed.
11) Stop.
C++ program implementation:
Example Output:
Enter the size of the square: 5 * * * * * * * * * * * * * * * *
Code Explanation:
First, we create a variable:
int n;
This stores the size of the square.
Then:
cin >> n;
reads the number entered by the user.
If the user enters:
5
The program needs to print a square with 5 rows and 5 columns.
The outer Loop
for (int i = 1; i <= n; i++)
This loop handles the rows.
For n = 5, it runs like this:
i = 1 → Row 1
i = 2 → Row 2
i = 3 → Row 3
i = 4 → Row 4
i = 5 → Row 5
So every time the outer loop runs, the program starts working on a new
row.
The inner Loop
Inside the outer loop, we have:
for (int j = 1; j <= n; j++)
This loop checks every position in the current row.
For example, in row 2:
j = 1 → First position
j = 2 → Second position
j = 3 → Third position
j = 4 → Fourth position
j = 5 → Last position
At each position, the program checks:
if (i == 1 || i == n || j == 1 || j == n)
If the position is on the border, it prints:
cout << "* ";
Otherwise, it prints:
cout << " ";
The two spaces keep the empty part inside the square.
After one row is complete:
cout << endl;
moves to the next line.
Then the next row starts.
Loop Dry Run:
Let's understand the program with:
n = 5
Row 1
Here:
i = 1
The condition:
i == 1
is true.
So every position in the first row prints a star.
* * * * *
Row 2
Now:
i = 2
The first position has:
j = 1
Since j == 1, it is on the border.
Print:
*
For j = 2, j = 3, j = 4, the positions are inside the square.
So spaces are printed.
When:
j = 5
j == n is true.
So another star is printed.
The complete row becomes:
* *
Row 3 and Row 4
The same logic happens again:
* *
* *
Row 5
Here:
i = 5
Since i == n, this is the last row.
Every position prints a star:
* * * * *
So the complete output is:
* * * * * * * * * * * * * * * *
Time & Space Complexity:
Time Complexity: O(n²)
The outer loop runs n times.
For every row, the inner loop also runs n times.
So the program checks:
n × n = n²
Positions.
Therefore:
Time Complexity = O(n²)
Space Complexity O(1)
The program only uses a few variables such as:
n
i
j
It does not use an array or store the pattern in memory.
Therefore:
Space Complexity = O(1)
Common Mistakes:
Mistake 1: Forgetting the last column
Some beginners write only
if (i == 1 || i == n || j == 1)
This misses the last column.
You also need:
j == n
The complete condition is:
i == 1 || i == n || j == 1 || j == n
Mistake 2: Using && Instead of ||
For this pattern, use OR:
||
Do not replace all conditions with AND:
&&
We want to print a star if the position matches any one border
condition.
Mistake 3: Printing the Wrong Number of Spaces
The star is printed with:
"* "
So the empty part should also use two spaces:
" "
This helps keep the shape of the square correct.
Mistake 4: Forgetting to Move to the Next Line
After completing each row, use:
cout << endl;
Without it, the entire pattern will print on one line.
Related Patterns:
After understanding the hollow square, you can try these next.
Solid Square
* * * * * * * * * * * * * * * * * * * * * * * * *
Hollow Rectangle
* * * * * * * * * * * * * * * *
Right Triangle
* * * * * * * * * * * * * * *
Number Square
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
The basic idea of loops remains similar. you mainly change the
conditions or what you print.
Frequently Asked Questions (FAQ's):
What is a hollow square pattern?
It is a square where stars are printed only on the outside border,
while the inside remains empty.
Why do we need nested loops?
One loop is used for rows, and another loop is used for positions
or columns inside each row.
What is the main condition for this pattern?
i == 1 || i == n || j == 1 || j == n
This checks whether the current position is on the border.
Can I use while loops?
Yes. You can create the same pattern using nested while loops. The border condition stays the same.
What happens when n = 1?
The output is:
*
Because the only position is also part of the border.
Conclusion:
The hollow square pattern becomes easy once you understand one idea:
print stars only on the border.
The first and last rows are completely filled with stars. in the
middle rows, only the first and last positions contain stars.
Nested loops help us move through every row and every position. The
if condition decides whether to print a star or leave the position empty.
The main condition to remember is:
i == 1 || i == n || j == 1 || j == n
Once this logic is clear, you can solve many other border based
patterns, including hollow rectangle and more advanced C++ pattern
problems.
Happy coding!

