This code prints a diamond shape made of asterisks using nested loops. The variable n
is set to 8, which determines the size of the diamond.
The first loop iterates from 1 to n
and prints spaces before each line of asterisks. The second loop prints the asterisks, increasing the number of asterisks per line until reaching n
.
The third loop iterates from n-1
to 1 and prints spaces before each line of asterisks. The fourth loop prints the asterisks, decreasing the number of asterisks per line from n
to the current line number.
Overall, this code produces a symmetrical diamond shape made of asterisks with n
rows.
#include <iostream>
using namespace std;
int main() {
int i, j, k, n = 8;
for (i = 1; i <= n; i++) {
for (j = 1; j < i; j++) {
cout << ' ';
}
for (k = i; k <= n; k++) {
cout << "* ";
}
cout << endl;
}
for (i = n - 1; i >= 1; i--) {
for (j = 1; j < i; j++) {
cout << ' ';
}
for (k = i; k <= n; k++) {
cout << "* ";
}
cout << endl;
}
return 0;
}
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *