ad

Sunday, April 13, 2025

C Program to Print Hollow Square Star Pattern


C Program 


Q. C Program to Print Hollow Square  Star Pattern 

Example



#include <stdio.h>

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

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

   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");
   }

   return 0;
}

C Program to Print Hollow Square  Star Pattern

Explain Program

Step :1

#include <stdio.h>

Standards Input Output library,  printf and scanf.

 Step :2

int main()

 entry point of the program.

i for iterating through rows,

 j for iterating through columns,

 N for the size of the square (number of rows and columns).

 Step :3

printf("Enter number of rows: ");

scanf("%d", &N);

 user to input a number (N), which defines the dimension of the square.

square will have N rows and N columns.

 Step :4

Outer Loop: 
Rows
for(i=1; i<=N; i++)
loop run from 1 to N (inclusive) to handlerow.

Step :5

Inner Loop:

 Columns

for(j=1; j<=N; j++)

 loop runs for each column within a row (again from 1 to N).

Step :6

Logic to Print the Border

if(i==1 || i==N || j==1 || j==N)

condition check current cell border of the square:

 First row: i == 1

 Last row: i == N

First column: j == 1

 Last column: j == N

 condition is true:

Step :7

printf("*");

Print an asterisk (*).

Step :8

printf(" ");

Print a space ( ) to create the hollow center.

Step :9

printf("\n");

After finishing row, move to the next line.

Step :10

 return 0;

 program finished successfully

Q. C Program to Print Hollow Square  Star Pattern

OutPut :-

C Program to Print Hollow Square  Star Pattern

Related Post :-

c program to print hourglass pattern

c program to print diamond pattern

c program to print rhombus star pattern


No comments:

Post a Comment