Question #65417

. Use forloops to construct a program that displays a pyramid of Xs on the screen. The pyramid should look like this
X
XXX
XXXXX
XXXXXXX
XXXXXXXXX

Expert's answer

const int n = 5;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 2*i + 1; j++)
cout << "X";
cout << endl;
}

2) Solution for normal pyramide
const int n = 5;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n-i-1; j++)
cout << " ";
for (int j = 0; j < 2 * i + 1; j++)
cout << "X";
cout << endl;
}
LATEST TUTORIALS
APPROVED BY CLIENTS