Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

QuestionDetails: the value of pi can be approximated by using the fllowinseries

ID: 3617993 • Letter: Q

Question

QuestionDetails: the value of pi can be approximated by using the fllowinseries
pi = 4(1-1/3+1/5-1/7+...)
write a value returning function nmed almostpi, which hasaninteger parameter n. The function computes and returns anapproximation for i which use n-many terms in the series.
For example, if n = 2, the function returns the value of4(1-1/3).
if n = 5, the following returns the valu of4(1-1/3+1/5-1/7+1/9).
the main prgam asks the user to input the value of n: How manytimes do you want in the approximation of pi?" Then it calls thefunction, and outputs the value which the function returns. QuestionDetails: the value of pi can be approximated by using the fllowinseries pi = 4(1-1/3+1/5-1/7+...) write a value returning function nmed almostpi, which hasaninteger parameter n. The function computes and returns anapproximation for i which use n-many terms in the series. For example, if n = 2, the function returns the value of4(1-1/3). if n = 5, the following returns the valu of4(1-1/3+1/5-1/7+1/9). the main prgam asks the user to input the value of n: How manytimes do you want in the approximation of pi?" Then it calls thefunction, and outputs the value which the function returns.

Explanation / Answer




#include<iostream> using namespace std;
double pvalue(int); int main() { int num; cout<<"Enter Last number for series: "; cin>>num; double result=pvalue(num);
cout<<"Value of pi is : "<<4*result; system("pause"); return 0; }
double pvalue(int n) { int flag=0; double sum=0.0; for(int v=1;v<=n;v=v+2) { if(flag%2==0) sum+=1/v; else sum-=1/v; flag++; }
return sum; }

#include<iostream> using namespace std;
double pvalue(int); int main() { int num; cout<<"Enter Last number for series: "; cin>>num; double result=pvalue(num);
cout<<"Value of pi is : "<<4*result; system("pause"); return 0; }
double pvalue(int n) { int flag=0; double sum=0.0; for(int v=1;v<=n;v=v+2) { if(flag%2==0) sum+=1/v; else sum-=1/v; flag++; }
return sum; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote