+ 8

How to create a pyramid using C++

I do manage but its actually a triangle not a pyramid? #include<iostream> using namespace std; int main(){ int x,y; char star = '*'; char space = ' p '; int temp; for(x=1; x <= 23; x++){ if((x%2) != 0){ for(y=1; y <= x ; y++){ cout << star; } cout << endl; } } return 0; }

14th Mar 2017, 7:58 AM
Andre van Rensburg
Andre van Rensburg - avatar
2 Answers
+ 7
I found a solution, you divide the space by 2: #include<iostream> using namespace std; int main(){ int x,y; char star = '*'; char space = ' p '; int temp; int numSpaces = 0; for(x=1; x <= 23; x++){ if((x%2) != 0){ numSpaces = (23 - x) / 2; // Calculate number of spaces to add for(int i = 0; i < numSpaces; i++) // Apply the spaces { cout << " "; } for(y=1; y <= x ; y++){ cout << star; } cout << endl; } } return 0; }
14th Mar 2017, 8:35 AM
Andre van Rensburg
Andre van Rensburg - avatar
+ 21
u can check my code named squr pattern
14th Mar 2017, 9:23 AM
Mansi Dagla
Mansi Dagla - avatar