Answer to Question #191126 in C++ for python

Question #191126

print a pyramid by using "setw" manipulator in C++


1
Expert's answer
2021-05-12T02:54:38-0400
#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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment