Hello I have to do an assignment for C that states the following:
Write a program that will estimate the value of PI from the infinite series:
π=4/1-4/3+4/5-4/7+4/9-4/11+⋯
The program will ask the user to enter an integer and then calculate the value of PI given that many terms. For example, if the user enters 4, then the program should estimate PI using the first four terms of the sequence, and output the result of the calculation:
π=4/1-4/3+4/5-4/7≅2.895238
* Notes: You only need to output the final floating point result. Also notice that the sequence switches from adding and subtracting each term. Also be sure your program can handle the 0 case.
Your solution must include a function that returns the estimated value of PI and takes as input the number of terms used to calculated PI
double estimatePI(int terms);
Also i have to solve this using a while statement.
With the help you guys have given me I compounded the program and I am not getting any errors but whenever I run it and enter different integers, I always get 3.0000000000000000 as my answer unless i enter 0 as the integer. Not sure where in the calculations I messed up.
What I have tried:
#include <stdio.h>
#include <stdlib.h>
double estimatePI(int terms);
int main()
{
int terms;
printf("How many times would you like to calculate the value of PI by\n"
"using the infinite series: ");
scanf("%d", &terms);
printf("The value of pi = %1.16lf\n", estimatePI(terms));
return 0;
}
double estimatePI( int terms)
{
double PI = 0;
double iterations = 1;
int i;
i = 1;
while ( i <= ( terms * 2))
{
PI = PI + iterations * (4/i);
iterations = -iterations;
i += 2;
}
return PI;
}