playground/c/pi-val.c

50 lines
867 B
C

// Originally written in 2015
#include<stdio.h>
int main()
{
int sign=1, n;
double i, pi=1;
printf("Enter the value of n: \n");
scanf("%d", &n);
for(i=3; i<n; i=i+2)
{
sign*=-1;
pi+=(sign*((double)(1/i)));
//printf("\n%lf", pi); // SAME AS pi=pi+( sign* ( (double)(1/i) ) );
}
pi*=4;
printf("The value of pi is: %lf\n", pi);
return 0;
}
/*
Enter the value of n:
10
The value of pi is: 3.339683
Enter the value of n:
1000
The value of pi is: 3.139593
Enter the value of n:
9999
The value of pi is: 3.141793
Enter the value of n:
99999
The value of pi is: 3.141613
Enter the value of n:
999999
The value of pi is: 3.141595
Enter the value of n:
9999999
The value of pi is: 3.141593
Enter the value of n:
999999999
The value of pi is: 3.141593
*/