Write a c program to print a hollow square star pattern with diagonal using loops (for and while loop)
ADVERTISEMENTS
Write a c program to print a hollow square star pattern with diagonal using loops (for and while loop). In this program, you will learn how to print a hollow square star pattern with diagonal using loops.
Take an example to print this pattern through a c program:
// Write a c program to print hollow square
// star pattern with diagonal using loops
#include <stdio.h>
int main() {
int x = 0, y = 0;
unsigned int sS = 0;
printf("-----Enter the side of a square-----\n");
scanf("%u", &sS);
printf("\n");
for (x = 1; x <= sS; ++x) {
for (y = 1; y <= sS; ++y) {
// It will check boundary conditions
// It will check diagonal and secondary diagonal conditions
if ((x == 1) || (x == sS) || (y == 1) || (y == sS) || (x == y) || (y == (sS - x + 1)))
printf("*");
else
printf(" ");
}
// It will print the new line
printf("\n");
}
return 0;
}
Output
-----Enter the side of a square-----
12
************
** **
* * * *
* * * *
* * * *
* ** *
* ** *
* * * *
* * * *
* * * *
** **
************