Write two C programs that will compute an approximation to the constant pi, usin
ID: 3566468 • Letter: W
Question
Write two C programs that will compute an approximation to the constant pi, using series A1 and A2 in the image below. The programs should take the number of terms to compute as input parameter. Include a comment to tell how many terms are needed to give six digits of accuracy to the right of the decimal point.
Write two C programs that will compute an approximation to the constant pi, using series A1 and A2 in the image below. The programs should take the number of terms to compute as input parameter. Include a comment to tell how many terms are needed to give six digits of accuracy to the right of the decimal point.Explanation / Answer
Method A1
#include<stdio.h>
#include<math.h>
void valueOfPi(int n){
double sum = 0;
double res;
double temp;
int i;
for(i=0; i<n; i++){
temp = ((2*i + 1)*(2*i + 1));
sum += 1/temp;
}
res = sqrt(8*sum);
printf("The value of Pi taken from %d terms is %f ",n,res);
}
int main(){
int n;
printf("Enter value of n : ");
scanf("%d",&n);
valueOfPi(n);
return 0;
}
Method A2
#include<stdio.h>
#include<math.h>
void valueOfPi(int n){
double sum = 0;
double res;
double temp;
int i;
for(i=0; i<n; i++){
temp = (2*i + 1);
if(i%2 == 0){
sum += 1/temp;
}else{
sum -= 1/temp;
}
}
res = 4*sum;
printf("The value of Pi taken from %d terms is %f ",n,res);
}
int main(){
int n;
printf("Enter value of n : ");
scanf("%d",&n);
valueOfPi(n);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.