Logic to print hollow rectangle star pattern.
***************
*             *
*             *
*             *
***************

C Programming Language Loop control in C Language (Article) Loop control in C Language (Program)

2304

Program:

/**
 * 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;
}

Output:

Enter number of rows: 5 
Enter number of columns: 10
**********
*        *
*        *
*        *
**********

Explanation:

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.

  1. Input number of rows and columns from user. Store it in a variable say rows and columns.
  2. To iterate through rows, run an outer loop from 1 to rows. Define a loop with structure for(i=1; i<=rows; i++).
  3. To iterate through columns, run an inner loop from 1 to columns. Define loop with structure for(j=1; j<=columns; j++).
  4. 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.
  5. After printing all columns of a row, move to next line i.e. print new line after inner loop.

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.