Can anybody tell me how can i calculate x decimal numbers of pi, if my input is 2 than output should be this 3.14 if my input is 4 then output is 3.141. I have tried code like this but doesnt work how i want.

pi +=3+4/(k*l*m) - 4/(a*b*c); //+ 4/(6*7*8) - 4/(8*9*10) + 4/(10*11*12) - 4/(12*13*14)+4/(15*16*17);

thanks in advance for help.

You should maybe finish the thread you started 5 days ago on the exact same question. What was lacking in the answers you got then.

Just use this:

 double calc_pi(int nrDigitals)
{
    double a = 1;
    double b = 1 / sqrt(2);
    double t =  (double)1 / 4;
    double p = 1;

    double temp_a;
    double temp_b;

    for(int i = 0; i < nrDigitals; ++i)
    {
        temp_a = (a + b) / 2;
        temp_b = sqrt(a * b);
        t = t - p * pow((a - temp_a), 2);
        p = 2 * p;

        a = temp_a;
        b = temp_b;
    }

    double pi = pow((a + b), 2) / (4 * t);
    return pi;
}

It's strange that you get only 5, i get 14 but that depends on the compiler you are using. Anyway, if you want to get really many digits you will have to use a string(or char[]) instead of double and implement the addition, pow, sqrt, multiplication and divide for numbers represented as string. For that you have to comeback to basic math, but that is something that you should do for yourself. The algorithm stays the same, just that you must use different data types and implement the operations and that is true for any algorithm that you choose and expect a large number output!

This article has been dead for over six months. Start a new discussion instead.