I need help with a C++ problem
π= 4(1/1- 1/3+ 1/5- 1/7+ 1/9- 1/11+ … 〖(-1)^n /(2n+1)+ …)
How many terms of series (1) you need to use before you get the approximation p of π, where:
a) p = 3.0
b) p = 3.1
c) p = 3.14
d) p = 3.141
e) p = 3.1415
f) p = 3.14159
I need to use the do-while loop or maybe the for loop. Please help me.
This is what i was trying to do but is not right..
#include <iostream>
using namespace std;
int main()
{
double c;
double i, a=-1, b;
double s=0, p;
cout << "Please enter a value for p." << endl;
cin >> p;
do
{
c=4;
a= -a;
b= 2*i+1;
s+= c*(a/b);
i++;
}
while (s<p+0.05 && s>p-0.05); // i think the condition is wrong.
cout << "The value of i is: " << i << endl;
return 0;
}