Print Diamond Pattern
C++
Hard
4 views
Problem Description
Print diamond shape using stars.
Real Life: Making decorative patterns.
Step-by-Step Logic:
1. Divide into two parts: upper and lower
2. Upper part: spaces decrease, stars increase
3. Lower part: spaces increase, stars decrease
4. Use nested loops for spaces and stars
Official Solution
void control_q13_diamond_pattern() {
int rows = 5;
// Upper part
for(int i = 1; i <= rows; i++) {
// Print spaces
for(int j = 1; j <= rows - i; j++) {
cout << " ";
}
// Print stars
for(int k = 1; k <= 2*i - 1; k++) {
cout << "*";
}
cout << endl;
}
// Lower part
for(int i = rows - 1; i >= 1; i--) {
// Print spaces
for(int j = 1; j <= rows - 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!