Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- long double approx1(int n);
- long double approx2(int n);
- long double approx3(int n);
- int main()
- {
- int num1;
- num1 = 0;
- printf("Enter number of terms to approximate pi to, or enter 0 to terminate: ");
- scanf("%d", &num1);
- puts(" ");
- while (num1 != 0)
- {
- if(num1 == 1)
- { printf("First approximation of pi to %d term is %.8Lf \n\n", num1, approx1(num1));
- printf("Second approximation of pi to %d term is %.8Lf \n\n", num1, approx2(num1));
- printf("Third approximation of pi to %d term is %.8Lf \n\n", num1, approx3(num1));
- }
- else if (num1 > 1)
- { printf("First approximation of pi to %d terms is %.8Lf \n\n", num1, approx1(num1));
- printf("Second approximation of pi to %d terms is %.8Lf \n\n", num1, approx2(num1));
- printf("Third approximation of pi to %d terms is %.8Lf \n\n", num1, approx3(num1));
- }
- else
- printf("Wrong input, please try again. \n");
- printf("Enter number of terms to approximate pi to, or enter 0 to terminate: ");
- scanf("%d", &num1);
- }
- printf("**Program Terminated***");
- return 0;
- }
- long double approx1(int n)
- {
- long double pi, num, denom;
- pi = 0;
- denom = 1;
- num = 4;
- int i;
- for ( i = 1; i <= n; i = i + 1 )
- {
- if ( i % 2 != 0 ){
- pi = pi + num / denom;
- }
- else {
- pi = pi - num / denom;
- }
- denom = denom + 2.0;
- }
- return pi;
- }
- long double approx2(int n)
- {
- long double pi, num, denom;
- pi = 0;
- denom = 1;
- num = 1;
- int i;
- for ( i = 1; i <= n; i = i + 1 )
- {
- if (i % 2 != 0)
- {
- pi = pi + (12 * (num / (powl(denom, 2.0))));
- }
- else {
- pi = pi - (12 * (num / (powl(denom, 2.0))));
- }
- denom = denom + 1.0;
- }
- pi = sqrtl(pi);
- return pi;
- }
- long double approx3(int n)
- {
- long double pi, num, denom, c1;
- pi = 0;
- num = 1;
- denom = 1;
- c1 = sqrtl(12.0);
- int i;
- for ( i = 0; i <= n; i = i + 1 )
- {
- if (i % 2 !=0)
- {
- pi = pi - (num/(denom*(powl(3.0, i))));
- }
- else {
- pi = pi + (num/(denom*(powl(3.0, i))));
- }
- denom = denom + 2.0;
- }
- return pi*c1;
- }
Add Comment
Please, Sign In to add comment