Print Hollow Diamond Pattern
C++
Hard
3 views
Problem Description
Print diamond with only border.
Real Life: Combining hollow and diamond concepts.
Step-by-Step Logic:
1. Upper half: spaces, star, spaces, star
2. Lower half: mirror of upper
3. Only print stars at boundaries
4. Fill middle with spaces
Official Solution
void pattern_q14_hollow_diamond() {
int n = 5;
// Upper half
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n - i; j++) {
cout << " ";
}
for(int k = 1; k <= 2*i - 1; k++) {
if(k == 1 || k == 2*i - 1) {
cout << "*";
} else {
cout << " ";
}
}
cout << endl;
}
// Lower half
for(int i = n - 1; i >= 1; i--) {
for(int j = 1; j <= n - i; j++) {
cout << " ";
}
for(int k = 1; k <= 2*i - 1; k++) {
if(k == 1 || k == 2*i - 1) {
cout << "*";
} else {
cout << " ";
}
}
cout << endl;
}
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!