C Program to Print Hollow Square Pattern Using While Loop
Complete guide with code, output, and easy-to-follow explanation for beginners
About the Author
Sankalan Data Technologies
Programming & Data Analytics Tutorials
Published by Sankalan Data Technologies. Our tutorials and data guides are independently developed and curated to provide practical, hands-on learning resources for Python, SQL, and data analytics practitioners.
Quick Answer: Hollow Square Pattern in C
To print a hollow square in C, use two loops — one for rows and one for columns.
Print a star (*) only when you are on the border, otherwise print a space.
#include <stdio.h>
int main() {
int i, j, N;
printf("Enter number of rows: ");
scanf("%d", &N);
i = 1;
while (i <= N) {
j = 1;
while (j <= N) {
if (i == 1 || i == N || j == 1 || j == N)
printf("*");
else
printf(" ");
j++;
}
printf("\n");
i++;
}
return 0;
}
Output (for N=5):
*****
* *
* *
* *
*****
What You'll Learn in This Tutorial
- What is a Hollow Square Pattern?
- Logic Behind the Pattern
- Complete C Program
- Program Output
- Step-by-Step Explanation
- Practice Questions
What is a Hollow Square Pattern?
A hollow square pattern is a grid of stars where only the outer border is filled with stars — the inside remains empty. It is one of the most common pattern printing exercises in C because it teaches you how to work with nested loops and conditions inside them.
The pattern looks like a square frame made of stars. If the user enters 5, you get a 5×5 square with an empty middle:
***** * * * * * * *****
Why Learn This Pattern?
- Practices nested loops — You learn how two loops work together
- Teaches condition-based printing — Print different characters at different positions
- Common in exams and interviews — Pattern questions are frequently asked
- Builds problem-solving skills — You translate a visual idea into logic
Logic Behind the Pattern
The idea is simple. Imagine a grid of rows and columns. Each cell can hold either a star or a space. The rule is:
Rule for Printing
Print a star if the current cell is:
- On the first row (i == 1)
- On the last row (i == N)
- On the first column (j == 1)
- On the last column (j == N)
Otherwise, print a space.
This rule is easy to remember. If you are on the boundary of the square, you print a star. If you are inside the square, you print a space.
Tip: The condition i == 1 || i == N || j == 1 || j == N
is the heart of the program. Once you understand this single line, the entire pattern
becomes clear.
Complete C Program
C Program to Print Hollow Square Using While Loop
/**
* Program in C to print hollow square star pattern
* using while loop
*/
#include <stdio.h>
int main() {
int i, j, N;
/* Ask the user for the number of rows */
printf("Enter number of rows: ");
scanf("%d", &N);
/* Loop through each row */
i = 1;
while (i <= N) {
/* Loop through each column */
j = 1;
while (j <= N) {
/* Print star for border cells, space for inner cells */
if (i == 1 || i == N || j == 1 || j == N) {
printf("*");
} else {
printf(" ");
}
j++;
}
/* Move to the next line */
printf("\n");
i++;
}
return 0;
}
Program Output
When the user enters 5, the program prints:
Enter number of rows: 5
*****
* *
* *
* *
*****
When the user enters 7, the pattern becomes:
Enter number of rows: 7
*******
* *
* *
* *
* *
* *
*******
Step-by-Step Explanation
-
Declare variables: We use
ifor the row counter,jfor the column counter, andNto store the user's input. -
Read input: The program asks the user how many rows they want.
The value is stored in
N. -
Outer while loop: This loop runs from
i = 1toi = N, once for each row. -
Inner while loop: Inside each row, another loop runs from
j = 1toj = N, once for each column. - Condition check: Inside the inner loop, we check whether the current position is on the border. If it is, we print a star; otherwise, we print a space.
-
Next line: After the inner loop finishes, we print a newline
character (
\n) to move to the next row. - Repeat: The outer loop continues until all rows are printed.
Visual Walkthrough (N = 5)
Row 1: * * * * * → all stars (border row) Row 2: * * → stars on ends, spaces inside Row 3: * * → stars on ends, spaces inside Row 4: * * → stars on ends, spaces inside Row 5: * * * * * → all stars (border row)
Common Mistakes to Avoid
- Using
&&instead of||— This checks if the cell is on the corner only, not the entire border. - Forgetting
j++inside the inner loop — This creates an infinite loop. - Forgetting
i++inside the outer loop — This also causes an infinite loop. - Missing newline after each row — The output would print on a single line.
- Starting the loop from 0 — If rows start at 0, the condition must use
0andN-1instead of1andN.
Practice Questions
Once you understand this pattern, try these variations:
- Print a hollow rectangle with R rows and C columns.
- Print a hollow square using
#instead of*. - Print a hollow square with a diagonal inside.
- Print a solid square pattern for comparison.
- Print the same hollow square using a do-while loop.
- Print a hollow square where the user chooses both the border character and the inside character.
Hint: For each variation, the border condition stays the same. Only the print statements or the loop structure changes.
Why This Pattern Matters
- Foundation for nested loops: Most pattern problems use nested loops. This one is a great starting point.
- Common in exams: Pattern printing questions are asked in BCA, MCA, and engineering practical exams.
- Interview favourite: Interviewers often ask you to write a pattern program to check loop logic.
- Builds visual thinking: You learn to translate a picture into code.
Frequently Asked Questions
How do I print a hollow square star pattern in C?
Use nested loops. The outer loop controls rows and the inner loop controls columns. Print a star only when the row or column is on the border (first or last), otherwise print a space.
Why is the star printed only on the border in a hollow square pattern?
The hollow square pattern only shows stars on the outer boundary. The condition i == 1 || i == N || j == 1 || j == N checks if the current position is on the first row, last row, first column, or last column. If yes, a star is printed; otherwise, a space is printed.
What is the difference between a solid square and a hollow square pattern?
A solid square prints a star in every position of the grid. A hollow square prints stars only on the outer border and leaves the inside empty (spaces).
Can I print a hollow rectangle instead of a hollow square?
Yes. A hollow square uses the same number of rows and columns (N × N). For a hollow rectangle, use different values for rows and columns, like R × C.
Can I use a while loop instead of a for loop for this pattern?
Yes. The same logic can be implemented using while loops. You need to initialize the counter before the loop, check the condition inside while, and increment the counter inside the loop body.
What happens if I forget to increment the counter?
Forgetting i++ or j++ creates an infinite loop. The program keeps printing the same row or the same character forever. Always make sure to update the counter inside the loop.
Related Tutorials
Previous Topic: Find the Sum of All Digits of a Given Number | Next Topic: Check if a Number is Perfect Using Do-While