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 <bits/stdc++.h>
using namespace std;
int main() {
int x = 0, y = 0;
unsigned int sS = 0;
cout << "-----Enter the side of a square-----\n";
cin >> sS;
cout << endl;
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)))
cout << "*";
else
cout << " ";
}
// It will print the new line
cout << endl;
}
return 0;
}
Output
-----Enter the side of a square-----
12
************
** **
* * * *
* * * *
* * * *
* ** *
* ** *
* * * *
* * * *
* * * *
** **
************