/* TMA02 Q4 */
/* Program by Selina Sze Min Na */

#include <stdio.h>
#include <math.h>

float PI =0;

/* Evaluate the value PI by input a value "n" to the formula for counting purpose*/
double evaluatePI(long n)
{
    /* the variable sum is assigned to the sum of value 1/((2*i-1)*(2*i-1))*/
	double sum = 0;

    /* the variable i is the number of term for counting */
	for (int i = 1; i <= n; i++)
	{
		sum += 1/(double)pow(2*i-1,2);
	}

    /*the function sqrt() is to count out the square root of a variable*/
    return sqrt(8*sum);
}

main()
{
    printf("*** Program by Selina Sze Min Na, student ID: 00545257 ***\n ");
    printf("\t*** Date of Authoring: 4 Feb 2001 ***\n");
    printf("The prupose of this program is to test the reliability of new CPU.\n");
    printf("By evaluating the value of PI with the CPU and check for discrepancies.\n\n");

    long n = 0;
    double PI;

    while (n >= 0)
    {
        printf("Enter the number of terms (-1 to quit): ");
        scanf("%d", &n);

        /*Called function with passing the variable n in it for calculating the value PI.*/
        PI = evaluatePI(n);

        printf("pi = %0.20f\n", PI);
    }
}