In this article, I will show you, How to write a C program to print hollow square star pattern. Here, one thing is important to know that all sides of the square must be the same.
Logic to write C program to print hollow square star pattern:
It is very easy to print a hollow square star pattern in C, below I have mentioned a few steps to print a hollow square pattern in C:
- You must know the side of the square.
- There should be two-loop, inner and outer.
- Inner loop print star for first and last row or for the first and last column. Otherwise, it prints the space.
- After completing each iteration of the inner loop, the outer loop prints the new line.
See the, C program to print hollow square star pattern:
Here, I have mentioned two methods to print hollow square star pattern.
Method1:
#include <stdio.h>
int main()
{
int x = 0,y = 0;
unsigned int squareSide = 0;
printf("Enter Side of a Square = ");
scanf("%u",&squareSide);
for(x=1; x<=squareSide; ++x)
{
for(y=1; y<=squareSide; ++y)
{
if((x==1) || (x==squareSide) || (y==1) || (y==squareSide))
{
//Print star
printf("*");
}
else
{
//Print space
printf(" ");
}
}
// Print new line
printf("\n");
}
return 0;
}
Output:

Method2:
#include <stdio.h>
#define isBorder(x,y,n) ((x==1) || (x==n) || (y==1) || (y==n))
int main()
{
int x = 0,y = 0;
unsigned int squareSide = 0;
printf("Enter Side of a Square = ");
scanf("%u",&squareSide);
for(x=1; x<=squareSide; ++x)
{
for(y=1; y<=squareSide; ++y)
{
isBorder(x,y,squareSide)? printf("*"):printf(" ");
}
// Print new line
printf("\n");
}
return 0;
}
Output:

