print a pyramid by using "setw" manipulator in C++
#include <iostream>
#include <iomanip>
using namespace std;
//setw(length)
//setfill(char)
int height; //Number of height.
int i;
int main()
{
cout << "Enter height: ";
cin >> height;
//upside down triangle
for (int i=height; i>=1; i--){ //Start with given height and decrement until 1
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
cout<< "\n"; //line break between
//rightside up triangle
for (int i=1; i<=height; i++){ //Start with 1 and increment until given height
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
cout<< "\n";
//right aligned triangle
for (int i=1; i<=height; i++){ //Start with 1 and increment until given height
cout << setfill (' ') << setw(i-height) << " ";
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
cout<< "\n";
//upside down/ right aligned triangle
for (int i=height; i>=1; i--){ //Start with given height and decrement until 1
cout << setfill (' ') << setw(height-i+1) << " ";
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
cout<< "\n";
//PYRAMID
for (int i=1; i<=height; i++){ //Start with 1 and increment until given height
cout << setfill (' ') << setw(height-i*3) << " "; //last " " is space between
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
}//end of main
Comments
Leave a comment