C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C programs

C program for 'Hollow Square' Pattern

In this C program, we are going to create, print a Hollow Square using asterisks and loops (nested loops).
Submitted by Shamikh Faraz, on February 16, 2018

In this program we will learn, how we can print hollow square using ‘asterisks’ and 'loops'?

C program


int main(){
	int rows, a, b;
	printf("Enter number of rows\n");
	scanf("%d", &rows);

	/*this loop increase number of rows */
	for(a = 0; a < rows; a++)  
		/*this loop increase number of columns */
		for(b = 0; b < rows; b++) 
			/* this puts the star on boundaries */
			if(a==0 || a==rows-1 || b==0 || b==rows-1) 
				printf("*"); //prints the star
				printf(" "); /* leaves the middle columns of square blank*/
	return 0;


Output - Hollow square

You may also be interested in...

C/C++ Tips and Tricks...

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.

Comments and Discussions