Print Hollow Square Pattern
C++
Medium
3 views
Problem Description
Print square with only border stars.
Real Life: Understanding boundary detection.
Step-by-Step Logic:
1. Print star if first or last row
2. Print star if first or last column
3. Otherwise print space
4. Creates hollow effect
Official Solution
void pattern_q9_hollow_square() {
int n = 5;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(i == 1 || i == n || j == 1 || j == n) {
cout << "* ";
} else {
cout << " ";
}
}
cout << endl;
}
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!