This program prints a diamond shape made of stars (*) and vertical bars (|). It uses nested loops to iterate over the rows and columns of the diamond.
#include <iostream>
using namespace std;
int main() {
int n = 10, i, j, k;
for (i = 0; i < n; i++) {
for (j = 1; j <= n - i; j++) {
cout << " ";
}
cout << "*";
for (k = 0; k <= i - 1; k++) {
cout << "|";
}
for (j = 1; j < i; j++) {
cout << "|";
}
if (i > 0) {
cout << "*";
}
cout << endl;
}
}
*
*|*
*|||*
*|||||*
*|||||||*
*|||||||||*
*|||||||||||*
*|||||||||||||*
*|||||||||||||||*
*|||||||||||||||||*
Here’s a breakdown of how the program works:
n
to set the size of the diamond.for
loop to iterate over the rows of the diamond, from 0 to n-1
.for
loop to print spaces before the first star on each row. The number of spaces printed depends on the current row number (i).for
loop to print vertical bars before the center of the diamond. The number of vertical bars printed depends on the current row number (i).for
loop to print vertical bars after the center of the diamond. The number of vertical bars printed depends on the current row number (i).