I am trying to write a code that finds pi accurately. When I run my code I get an output of 1.000000 every time because n
always equals 0 in the equation to find add
and minus
after the first equation but when I print it, n
is increasing as it should. I don't get any error messages because there are no visible problems with the code that I have. This is a conversion from the same code I wrote in python and I don't know if that is meaning I have forgotten something.
#include <stdio.h>
int main()
{
int n = 1, iterations, times;
long double pi = 0.0, add, minus;
printf("Number of iterations: ");
scanf("%d", &iterations);
for (times = 1; times <= iterations; ++times)
{
add = 1 / n;
printf("%Lf \n", add);
pi = pi + add;
n = n + 2;
minus = 1 / n;
printf("%Lf \n", minus);
pi = pi - minus;
n = n + 2;
printf("%d \n", n);
printf("%Lf \n", pi);
}
pi *= 4;
printf("Pi = %Lf \n", pi);
return 0;
}