First of all, I'm new to coding and C++. I did my researches for my problem on the internet but the solutions are not quite worked out for me.
I'm trying to get approximation of Pi with Liebniz formula which is: Pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9...
The code is compilable and runnable. The problem is I get 4 as answer for every number of iterations (n value). Here is the main part of the code.
int main() {
int i ;
double a=1 ;
double pi4 = 0 ;
long n;
cout << "Number of iterations? ";
cin >> n;
for (i=1; i <= (n) ; i += 2) {
pi4 = pi4 + a * (1 / i);
a = -a;
}
cout.precision(20);
cout << "Pi = " << (pi4 * 4) << endl;
return 0;