Diamond Pattern in C++
Diamond in C++: full pyramid plus inverted from rows-1. Input 5 is 9 lines, not 5.
1#include <iostream>2using namespace std;34int main() {5 int rows;67 cout << "Enter number of rows (half): ";8 cin >> rows;910 // Upper part of diamond11 for (int i = 1; i <= rows; i++) {12 for (int j = 1; j <= rows - i; j++) {13 cout << " ";14 }15 for (int j = 1; j <= 2 * i - 1; j++) {16 cout << "*";17 }18 cout << endl;19 }2021 // Lower part of diamond22 for (int i = rows - 1; i >= 1; i--) {23 for (int j = 1; j <= rows - i; j++) {24 cout << " ";25 }26 for (int j = 1; j <= 2 * i - 1; j++) {27 cout << "*";28 }29 cout << endl;30 }3132 return 0;33}
Output
Enter number of rows (half): 5
*
***
*****
*******
*********
*******
*****
***
*What's going on
A diamond is a full pyramid glued to an inverted pyramid, without printing the fat middle twice.
cin >> rows; is the half-height. Sample 5 → 9 lines total (5 up, 4 down).
Upper half — identical to Full Pyramid:
spaces rows - i, stars 2 * i - 1. i = 1..rows → 1, 3, 5, 7, 9 stars.
Lower half:
for (int i = rows - 1; i >= 1; i--)
Start at rows - 1, not rows. Start at rows and the 9-star line prints twice and it looks like a stretched hexagon.
Same space/star formulas. i = 4, 3, 2, 1 → 7, 5, 3, 1 stars.
Prompt says "half". Type 5 expecting 5 lines and you'll get 9. That's the API.
Hollow diamond is the same two loops with the hollow if on stars. This file is solid.