C program to print hollow square or rectangular star pattern

Previous Pattern Next Pattern
Write a C program to print hollow square or rectangle star(*) pattern series using for loop. How to print hollow square series pattern of N rows using for loop in C programming. How to print hollow rectangular series pattern of M rows and N columns using loop in C programming. Logic to print hollow square or rectangle star pattern series in C program.

Example:
Input N: 5
Output:

Required knowledge

Basic C programming, If else, For loop

Logic to print hollow square star pattern

Before diving into the logic of this pattern, I highly recommend you to learn how to print simple square star pattern. As logic of this pattern is based on simple square star pattern.

Now talking about the pattern, it consists of N rows (where N is the total row to be printed). Each row contains exactly N columns. Also notice that here stars(*) only gets printed for first and last column or star(*) gets printed for each column for first and last row.

Step-by-step descriptive logic:
  1. To iterate through rows, run an outer loop from 1 to N (where N is the total rows to be printed).
  2. To print columns, run an inner loop from 1 to N.
    Inside this loop print star if i==1 OR i==N (where i is the current row number) else if j==1 OR j==N (where j is the current column number), otherwise print single space.
And finally you are done, lets now implement this:

Program to print hollow square pattern

/**
 * C program to print hollow square star pattern
 */

#include <stdio.h>

int main()
{
    int i, j, N;

    //Reads number of rows from user
    printf("Enter total rows : ");
    scanf("%d", &N);

    //Iterates over each row one by one
    for(i=1; i<=N; i++)
    {
        //Iterates over each column of the i-th row
        for(j=1; j<=N; j++)
        {
            if(i==1 || i==N || j==1 || j==N)
            {
                //Print star for first and last row and for first and last column
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }

        //Move to the next line/row
        printf("\n");
    }

    return 0;
} 


Output
Enter total rows: 5
*****
*   *
*   *
*   *
*****


Square star pattern screenshot

Hollow square star pattern in C


Logic to print hollow rectangle star pattern

Logic to print hollow rectangle star pattern is similar as hollow square star pattern that we just did. The only difference between both is the hollow square pattern consists of NxN matrix whereas hollow rectangle pattern consists MxN matrix.

Step-by-step descriptive logic:
  1. To iterate through rows, run an outer loop from 1 to M (where M is the total number of rows to be printed).
  2. To print columns, run an inner loop from 1 to N (where N is the total columns to be printed per row).
    Inside this loop print star (*) if it is first or last row, or if its if first or last column, otherwise print blank space.


Program to print hollow rectangle star pattern

/**
 * C program to print hollow rectangle star pattern
 */

#include <stdio.h>

int main()
{
    int i, j, M, N;

    //Reads number of rows and columns from user
    printf("Enter number of rows: ");
    scanf("%d", &M);
    printf("Enter number of columns: ");
    scanf("%d", &N);

    //Iterates over each row one by one
    for(i=1; i<=M; i++)
    {
        //Iterates over each column of the i-th row
        for(j=1; j<=N; j++)
        {
            if(i==1 || i==M || j==1 || j==N)
            {
                //Print star for first and last row and for first and last column
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }

        //Move to the next line/row
        printf("\n");
    }

    return 0;
} 


Hollow rectangle star pattern screenshot

Hollow rectangle star pattern in C


Happy coding ;)


Previous Pattern Next Pattern
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

4 comments:

  1. can u please tell me how to print the hollow squares in a row?

    ReplyDelete
    Replies
    1. The above program does the same. Means it prints hollow sqaure.

      Delete
    2. Or else you want to print more than one hollow stars in a row then you need to iterate

      for(i=1; i<=n; i++)
      {
      for(j=1; j<=n; j++)
      {
      if(i!=1 && i!=n && j!=1 && j!=n)
      {
      printf(" ");
      }
      else
      {
      printf("*");
      }
      }

      printf("\n");
      }


      into another outer for loop.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete