I have been asked to write a program that uses the formula series
∏ = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 +... to calculate the value of Pi given a specified number of terms.
The example output that I was given is giving me different output numbers than the program I have written.
Can someone please take a look at my code and help me figure out why I am getting a different output than I should be.
Here is the example output that I was given:
Program will approximate Pi
Enter the number of terms to use: 5
Display Pi after every how many steps? 1
RESULTS:
1: Pi = 4.000000000
2: Pi = 2.666666667
3: Pi = 3.466666667
4: Pi = 2.895238095
5: Pi = 3.339682540
Final Pi = 3.339682540
Press any key to continue . . .
I ran your code and it gave the right numbers but the output form was a little off. It skips the first case and reports one extra. This is because the output follows evaluation of the next term. Moving lines 41-45 to the top of the for loop fixed this. The other problem is that every output line starts with 1: (instead of 1: then 2: then 3: , etc.). Use i instead of steps in this line cout << steps << ": Pi = " << newPi << endl; which is presently line 44.