Print Pyramid Pattern (Centered)
C++
Medium
2 views
Problem Description
Print centered pyramid of stars.
Real Life: Classic pyramid shape.
Step-by-Step Logic:
1. Print spaces first for centering
2. Spaces = n - current row
3. Then print stars = 2*row - 1
4. Creates centered pyramid
Official Solution
void pattern_q6_pyramid() {
int n = 5;
for(int i = 1; i <= n; i++) {
// Print spaces
for(int j = 1; j <= n - i; j++) {
cout << " ";
}
// Print stars
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!