1) Write a program to produce all four triangles and the one pyramid similar to
ID: 3796167 • Letter: 1
Question
1) Write a program to produce all four triangles and the one pyramid similar to the output shownbelow. Your program should ask the user to enter the height of the triangle/pyramid anywhere from1 to 25, and prevent the user from entering a height any larger than 25.Hint: You can make each triangle with one for loop and clever use of the setfill() and setw()output manipulators. Make sure you include the iomanip library. An example is shown below:
cout << setw() << setfill(“*”) << “ ”;
(
Pi = 4* (1-1/3+1/5-1/7+......-1/(2n+1) +1/(2n +1))
)
Write a program that will approximate the value of using the above series evaluated at terms. Your program should ask the user to enter how many terms to use in theapproximation. Draw a flowchart for how your loop functions.Your program will undoubtedly require using a loop to approximate for an arbitrary number of terms. Notice that the sign of each successive term alternates from positive tonegative. As you approach this problem, ask yourself the following questions:
a) What’s the general pattern to calculate each term at a given ?b) What value does start at? c) Are the terms positive when is even, or when is odd?d) What code can I write to check if is even or odd each time through the loop?
f Log into Facebook Face x Facebook Login or sic x C Chegg study IGuded sex C 1)write program To Pix Fles C Secure I https mccd instructure.com/ courses/5 /files/folder/HW? preview 168440 comp250-hw3-loops-sp17. pdf 1 to 25, and prevent the user from entering a height any larger than 25 Hint: You can make each triangle with one for loop and clever use of the setfill() and setw() output manipulators. Make sure you include the iomanip library. An example is shown below: cout s (
Explanation / Answer
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
int num;
std::cout << "Enter height of triangle/pyramid :- ";
cin>>num;
if(num<=25){
//for 1st triangle
for(int i=0;i<num;i++){
for(int j=0;j<num-i;j++){
cout<<"*";
}
cout<<" ";
}
cout<<" ";
//for 2nd triangle
for(int i=0;i<num;i++){
for(int j=0;j<i+1;j++){
cout<<"*";
}
cout<<" ";
}
cout<<" ";
//for 3rd triangle
for(int i=0;i<num;i++){
cout<<setw(num-i);
for(int j=0;j<i+1;j++){
cout<<"*";
}
cout<<" ";
}
cout<<" ";
//for 4th triangle
for(int i=0;i<num;i++){
cout<<setw(i+1);
for(int j=0;j<num-i;j++){
cout<<"*";
}
cout<<" ";
}
cout<<" ";
//for pyramid
for(int i=0;i<num;i++){
cout<<setw(num-i);
for(int j=0;j<i+1;j++){
cout<<"*";
}
for(int k=0;k<i;k++){
cout<<"*";
}
cout<<" ";
}
}
else{
cout<<"Invalid Height";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.