C program to print hollow square or rectangle 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 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.

Example

Input

Enter number of rows: 5

Output

Required knowledge

Basic C programming, If else, For loop

Must have programming knowledge for this program.

Logic to print hollow square star pattern

Above pattern consists of N rows and N columns. Here stars(*) 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.

  1. Read number of rows to be printed from user. Store it in some variable say N.
  2. To iterate through rows, run an outer loop from 1 to N. For that define loop with structure for(i=1; i<=N; i++).
  3. To iterate through columns, run an inner loop from 1 to N Define loop with structure for(j=1; j<=N; j++).
  4. Inside inner loop print star for first and last row or for first and last column. Which is print star if i==1 OR i==N or j==1 OR j==N, otherwise print space.

Program to print hollow square pattern

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

#include <stdio.h>

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

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

    // Iterate over each row
    for(i=1; i<=N; i++)
    {
        //Iterate over each column
        for(j=1; j<=N; j++)
        {
            if(i==1 || i==N || j==1 || j==N)
            {
                // Print star for 1st, Nth row and column
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }

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

    return 0;
} 
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.

  1. Read number of rows and columns from user. Store it in some 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 OR i==rows or j==1 OR j==columns.

Program to print hollow rectangle star pattern

/**
 * 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/row
        printf("\n");
    }

    return 0;
} 
Output
Enter number of rows: 5 
Enter number of columns: 10
**********
*        *
*        *
*        *
**********

Happy coding ;)

Recommended posts

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
< pre>< code >----Your Source Code---- < / code > < / pre > (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