// INCLUDES
#include "stdafx.h"
#include <iostream>
usingnamespace std;
// MAIN
int main()
{
// This program will display a pyramid
for (int i = 1 ; i < 10 ; i+=2)
{
cout << "\n";
for (int j = 1 ; j <= i ; j++)
{
cout << "*";
}
}
cout << "\n\n";
// To Exit
cout << "Press ENTER to exit...";
cin.clear();
cin.sync();
cin.get();
return 0;
}
Just missing some logic. The top of the pyramid has one star and there is 9 spaces before that. Next line has 3 stars and there is 7 spaces before those 3 stars. To get the number of spaces to print before the first star you do (number_of_spaces_last_row_is_from_edge + length_of_pyramid).
So your last for-loop should come before line 13 and should just print the spaces
You have to add one more for loop to print out space before the stars.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int size = 10; // size of pyramid
int space = size;
for (int i = 1 ; i < size; i+=2 ){
cout << "\n";
// print the spaces
for (int j = 1 ; j < space ; j++){
cout << " ";
}
space--; // as we go down, we have less space to print
for (int j = 1 ; j <= i ; j++){
cout << "*";
}
}
#include <iostream>
usingnamespace std;
// MAIN
int main()
{
// This program will display a pyramid
int spaces = 10;
int stars = 1;
int size = 10;
int i = 0;
int j = 0;
int x = 0;
for (i = 0; i < size; i++)
{
for (j = 0; j <= spaces; j++)
{
cout << ' ';
}
spaces--;
for(x = 0; x < stars; x++)
{
cout << '*';
}
stars += 2;
cout << endl;
}
cout << "\n\n";
// To Exit
cout << "Press ENTER to exit...";
cin.ignore();
return 0;
}