Prints a pyramid shape with specified height using loop.
Here's the code to input a specific height of a pyramid shape using for and while looping,
Feel free to modify and analyze the flow.
Enjoy!
#include <iostream>
using namespace std;
int main()
{
int pyramid_height;
int line;
int count;
cout<<"Enter the Height of a pyramid: ";
cin>>pyramid_height;
while(pyramid_height > 30 || pyramid_height < 1)
{
cout<<"Pick another height (must be between 1 and 30): ";
cin>>pyramid_height;
}
cout<<endl;
for (line = 1 ; line <= pyramid_height ; line++)
{
int total_no_of_spaces = pyramid_height - line;
for (count = 1 ; count <= total_no_of_spaces ; count++)
cout<<' ';
for (count = 1 ; count < line * 2 ; count++)
cout<<'*';
cout<<endl;
}
return 0;
}
Feel free to modify and analyze the flow.
Enjoy!
#include <iostream>
using namespace std;
int main()
{
int pyramid_height;
int line;
int count;
cout<<"Enter the Height of a pyramid: ";
cin>>pyramid_height;
while(pyramid_height > 30 || pyramid_height < 1)
{
cout<<"Pick another height (must be between 1 and 30): ";
cin>>pyramid_height;
}
cout<<endl;
for (line = 1 ; line <= pyramid_height ; line++)
{
int total_no_of_spaces = pyramid_height - line;
for (count = 1 ; count <= total_no_of_spaces ; count++)
cout<<' ';
for (count = 1 ; count < line * 2 ; count++)
cout<<'*';
cout<<endl;
}
return 0;
}
Prints a pyramid shape with specified height using loop.
Reviewed by code-dev
on
1:57 PM
Rating:
No comments: