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);
           else
            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;
}

Recommended Answers

All 3 Replies

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++)

commented: keen eye +15

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.