Print Diamond Pattern
C++
Medium
3 views
Problem Description
Print complete diamond shape.
Real Life: Combining pyramid and inverted pyramid.
Step-by-Step Logic:
1. First print upper pyramid (n rows)
2. Then print lower inverted pyramid (n-1 rows)
3. Combine both parts
4. Forms diamond shape
Official Solution
void pattern_q8_diamond() {
int n = 5;
// Upper pyramid
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++) {
cout << "*";
}
cout << endl;
}
// Lower inverted pyramid
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++) {
cout << "*";
}
cout << endl;
}
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!