*************** * * * * * * ***************
/**
* C program to print hollow rectangle star pattern
*/
#include <stdio.h>
int main()
{
int i, j, rows, columns;
/* Input number of rows and columns from user */
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &columns);
/* Iterate over each row */
for(i=1; i<=rows; i++)
{
/* Iterate over each column */
for(j=1; j<=columns; j++)
{
if(i==1 || i==rows || j==1 || j==columns)
{
/* Print star for 1st and last row, column */
printf("*");
}
else
{
printf(" ");
}
}
/* Move to the next line */
printf("\n");
}
return 0;
}
Enter number of rows: 5
Enter number of columns: 10
**********
* *
* *
* *
**********
*************** * * * * * * ***************
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.
for(i=1; i<=rows; i++).for(j=1; j<=columns; j++).if(i==1 || i==rows || j==1 || j==columns) then print star otherwise space.First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.