playground/c/pi-val-leibnitz.c

42 lines
922 B
C

// Originally written in 2015
#include<stdio.h>
#define ACCURACY 1
int main()
{
int sign=1, n, ctr=0;
double i, pi=ACCURACY;
printf("Enter the value of n: ");
scanf("%d", &n);
for(i=3; i<n; i=i+2)
{
sign*=-1;
pi+=((sign*((double)(1/i)))*ACCURACY);
//printf("%.20lf\n", pi); // SAME AS pi=pi+( sign* ( (double)(1/i) ) );
}
pi*=4;
printf("The value of pi is: %.20lf\n", pi);
return 0;
}
/*
Enter the value of n: 1
The value of pi is: 4.00000000000000000000
Enter the value of n: 10
The value of pi is: 3.33968253968254025210
Enter the value of n: 100
The value of pi is: 3.12159465259101098766
Enter the value of n: 1000
The value of pi is: 3.13959265558978506405
Enter the value of n: 99999
The value of pi is: 3.14161265398978528651
Enter the value of n: 999999
The value of pi is: 3.14159465359369205473
*/