14,734,526 members
See more:
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;
}```
Posted
Updated 15-Mar-20 10:10am
v4
Patrice T 15-Mar-20 14:31pm

Now the list of errors.
Nelek 15-Mar-20 15:01pm

4/i might still be integer division. You might want to use 4.0/i to be sure

## Solution 1

You're overriding the value of your parameter, by defining a variable of the same name.
```double estimatePI( int terms)
{
double PI = 0;
double iterations = 1;
int i, terms;```

## Solution 2

In addition to Solution 1, I would remove
`estimatePI = PI;`

as it is not VB.
It gives
```    while ( i <= ( terms * 2))
{
PI = PI + iterations * (4/i);
iterations = -iterations;
i += 2;
estimatePI = PI;
}
return estimatePI;
}
```

[Update]
`return ;`

Re-read carefully what was written first time.

[Update]
By the way, (4/i) is an integer division which gives integer values.
v3
Member 14770192 15-Mar-20 14:05pm

Wow thank you guys for the really fast answers I appreciate it. I did what you guys suggested and it gives me less errors now but I am not sure what to set my return to in the function.
Patrice T 15-Mar-20 14:08pm

Use Improve question to update your question.
So that everyone can pay attention to this information.

Update your code and list errors.
Member 14770192 15-Mar-20 14:50pm

Wow that was really simple. Thank you for the help.