In this exercise, I will show you, How to write a C program to print hollow square star pattern.
Here, I have mentioned two methods to print hollow square star pattern.
#include <stdio.h> int main() { int x = 0,y = 0; unsigned int squareSide = 0; printf("Enter Side of a Square = "); scanf("%u",&squareSide); for(x=1; x<=squareSide; ++x) { for(y=1; y<=squareSide; ++y) { if((x==1) || (x==squareSide) || (y==1) || (y==squareSide)) { //Print star printf("*"); } else { //Print space printf(" "); } } // Print new line printf("\n"); } return 0; }
Output:
Enter Side of a Square = 10
**********
* *
----------------------------------------------------------------
#include <stdio.h> #define isBorder(x,y,n) ((x==1) || (x==n) || (y==1) || (y==n)) int main() { int x = 0,y = 0; unsigned int squareSide = 0; printf("Enter Side of a Square = "); scanf("%u",&squareSide); for(x=1; x<=squareSide; ++x) { for(y=1; y<=squareSide; ++y) { isBorder(x,y,squareSide)? printf("*"):printf(" "); } // Print new line printf("\n"); } return 0; }
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Here, I have mentioned two methods to print hollow square star pattern.
Method1:
Output:
Enter Side of a Square = 10
**********
* *
* *
* *
* *
* *
* *
* *
* *
**********
----------------------------------------------------------------
Method2:
Output:
Enter Side of a Square = 10
**********
* *
* *
* *
* *
* *
* *
* *
* *
**********
need an explanation for this answer? contact us directly to get an explanation for this answer