Quick links
Write a C program to print hollow square or rectangle star(*) pattern series using for loop. How to print hollow square or rectangle star pattern of N rows using for loop in C programming. Logic to print empty square or rectangle star pattern in C program.
Required knowledge
Basic C programming, If else, For loop, Nested loop
Logic to print hollow square star pattern
The above pattern is similar to square star pattern of N rows and N columns. Here star is printed only for first and last column or for first and last row.
Step by step descriptive logic to print empty square star pattern.
- Input number of rows to print from user. Store it in a variable say N.
- To iterate through rows, run an outer loop from 1 to N. For that define loop with structure
for(i=1; i<=N; i++)
. - To iterate through columns, run an inner loop from 1 to N. Define loop with structure
for(j=1; j<=N; j++)
. - Inside inner loop print star for first and last row or for first and last column. Which is print star if
i==1
ori==N
orj==1
orj==N
, otherwise print space. - After printing all columns of a row, move to next line i.e. print a blank line after inner loop.
Program to print hollow square pattern
Output
Enter number of rows: 5 ***** * * * * * * *****
Logic to print hollow rectangle star pattern
Logic to print hollow rectangle star pattern is similar to hollow square star pattern. The only difference is hollow square pattern is a NxN matrix whereas hollow rectangle pattern is a MxN matrix.
Step by step descriptive logic to print hollow rectangle star pattern.
- Input number of rows and columns from user. Store it in a variable say rows and columns.
- To iterate through rows, run an outer loop from 1 to rows. Define a loop with structure
for(i=1; i<=rows; i++)
. - To iterate through columns, run an inner loop from 1 to columns. Define loop with structure
for(j=1; j<=columns; j++)
. - Inside this loop print star for first or last row or for first or last column, otherwise print blank space. Which is
if(i==1 || i==rows || j==1 || j==columns)
then print star otherwise space. - After printing all columns of a row, move to next line i.e. print new line after inner loop.
Program to print hollow rectangle star pattern
Output
Enter number of rows: 5 Enter number of columns: 10 ********** * * * * * * **********
Happy coding