C Program To Calculate The pi By Maths Expression

How to write a C Program to calculate the pi by maths expression Using a array of willing value to calculated in C Programming Language ?

Solution:
/* Program to calculate the pi by math expression
  Using a array of willing value to calculated */
#include <stdio.h>
#include <stdlib.h>
#include <math.h> //to take the pi

double CountPi(int); //function to calculate the pi's expression

int main(void)
{
printf("Exactly value of pi to compare = %1.17f\n\n", M_PI);

int i = 0; //variable to count all willing results in to array
int N[6] = {1 ,2 ,10, 50, 100, 500}; //array with elements of willing number to calculate
double my_pi = 0.0; //the current pi by specific value

while(i < (sizeof(N) / sizeof(N[0]))) //until all the numbers of array calculated
{
double dif_pi = 0.0; //for the different of exactly pi
my_pi = CountPi(N[i]); //calculate the pi with specific value
dif_pi = my_pi - M_PI;
printf("The difference is %1.17f\n\n", dif_pi);
i++;
}
return 0;
}

/* Function to calculate the pi by math expresion */
double CountPi(int n) //n = N
{
int i = 1; //number to calculate until n

double sum = 0.0; //total addition of expression

double nume_1,s_fr1,deno_1; //nume_1->numerator , deno_1->denominator , s_fr1->fraction of math expression

while(i <= n) //addition until the willing value
{
nume_1 = pow(i - 0.5 , 2);
deno_1 = pow( n , 2);
s_fr1 = 1.0 + (nume_1 / deno_1);
sum += 1.0 / s_fr1;
i++;
}
sum *= 4;
sum /= n;
printf("pi(%d) = %1.17f\n", n, sum);
return sum; //returning the calculating value of pi
}


Learn More :