[c] found some old c progs

This commit is contained in:
Julin S 2023-04-30 12:22:46 +05:30
parent 4d708d1f6f
commit 94adfbce77
4 changed files with 151 additions and 0 deletions

6
c/README.org Normal file
View File

@ -0,0 +1,6 @@
#+TITLE: C
- [[./pi-val.c][pi-val.c]]: Approximating value of pi
- [[./pi-val-leibnitz.c][pi-val-leibnitz.c]]: Approximating value of pi using Ramanjuan's method
- [[./pi-val-ramanjuan.c][pi-val-ramanjuan.c]]: Approximating value of pi using Leibnitz's method

41
c/pi-val-leibnitz.c Normal file
View File

@ -0,0 +1,41 @@
// 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
*/

55
c/pi-val-ramanjuan.c Normal file
View File

@ -0,0 +1,55 @@
// Originally written in 2015
#include<stdio.h>
#include<math.h>
double pow1(int base, int power)
{
double p=1;
int j=0;
for(; j<power ; ++j)
{
p*=base;
}
return p;
}
double factorial(int a)
{
int j=a;
double f=1;
for(; j>1; --j)
{
f*=j;
}
return f;
}
int main()
{
int k, i;
double pi=0;
printf("Enter the value of k: ");
scanf("%d", &k);
for(i=0; i<k; ++i)
{
pi+=((factorial(4*i) * (1103 + (26390*i))) / (pow1(factorial(i), 4) * pow1(396, 4*i)) );
}
pi*=(double) ( ( 2 * sqrt(2) ) / ( 9801 ) );
pi = (double) ( 1 / pi );
printf("The value of pi using Ramanujan's equation is: %.20lf\n", pi);
return 0;
}
/*
Enter the value of k: 1
The value of pi using Ramanujan's equation is: 3.14159273001330552333
Enter the value of k: 2
The value of pi using Ramanujan's equation is: 3.14159265358979356009
Enter the value of k: 10
The value of pi using Ramanujan's equation is: 3.14159265358979311600
Enter the value of k: 100
The value of pi using Ramanujan's equation is: -nan
*/

49
c/pi-val.c Normal file
View File

@ -0,0 +1,49 @@
// 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
*/