Hello everyone,
I was given this assignment, but I am stuck. Your help is appreciated :)

1.Ask for a desired accuracy epsilon for the to approximate value of \pi.
2. Use the Leibniz series to approximate pi.
3.Check after each iteration step wether the value of the last summand | \frac{(-1)^n}{2n+1} | is smaller then the desired accuracy \epsilon and the iteration can end.
4.Print your approximation of \pi ( the Leibniz series will calculate \frac{\pi}{4} and not pi directly).
5. Only use basic arithmetic operations and define all floating point variables with data type double. Using the predefined function pow is not allowed!

#include <stdio.h>
int main ()
    int x;
    double sum=0;
    double PI,eps;
    int i;
    printf("Please Input the desired accuracy eps: ");
    scanf("%d", &eps);
    for (i=1; i<x; i++)

            if ((i%2)==1)
                sum=sum+1.0/((2.0 * (double)i) - 1.0);
            sum = sum - 1.0/((2.0 * (double)i) - 1.0);
            PI= 4*sum;
            printf("The sum is %f\n",sum);
            printf("Approximate value of PI is %9.f\n", sum);
            return 0;

Edited by senait.kifle.127

2 Years
Discussion Span
Last Post by cgeier

Oh one thing , this is sample output :)

Please input the desired accuracy eps: 0.00001

Approximation of PI is: 3.1415976536


In line #10, you trying to use x, but it has not been initialized (x doesn't have a value).

for (i=1; i<x; i++)

Edited by cgeier

keen eye

Also, line #20 should probably be:

printf("Approximate value of PI is %.9f\n", PI);

Changed from %9.f to %.9f and from sum to PI

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.