C Exercises

Aug 26, 2020

C program to print hollow square star pattern

 Ex: Write a C program to print hollow square star pattern. How to write a C program to print hollow square star pattern. C program to print hollow square star pattern.


Input from user:

Enter the number:

7


Expected output:

C program to print hollow square star pattern




  Step by step logic of the given program:

1. Accept input number from user declare variable say no.


2. Run outer loop from 1 to no which will shows rows of pattern:

for(i=1;i<=no;i++)


3. Run inner loop from 1 to no which will shows columns of pattern:

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


4. Using if statement give conditions to print stars:

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


5. Otherwise print spaces (to print it like hollow).


6. For next row use new line (\n).




Program:


#include<stdio.h>

void main()

{

int no,i,j;

printf("Enter the number:\n");

scanf("%d",&no);

/*Run loops from 1 to no*/

for(i=1;i<=no;i++)

{

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

{

/*Give conditions to print stars*/

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

{

printf("*");

}

else

{

/*Otherwise print spaces*/

                             printf(" ");

}

}

/*For new row*/

        printf("\n");

}

}



Above program shows the following output:


C program to print hollow square star pattern


No comments:

Post a Comment

If you have any doubts, please discuss here...👇