[Solved] Formula nested in loop won’t execute properly


First of all, make n and p integers.

Secondly n starts at 3 doesn’t it?

Third the Pi series is 4.0 (- fraction + fraction)…

Finally you can printf using %.*lf to increase/limit the precision of the output.

 if ( 2 == route )
 {
    printf("piseries calculator\n");

    double pi=4.0;
    int n,p;

    printf("define precision");
    scanf("%d",&p);

    for(n=3;n<p;n+=2)
    {
        pi -= 4.0/n;
        n+=2;
        pi += 4.0/n;
    }
    printf("%.*lf",p,pi);
    return 0;
}

1

solved Formula nested in loop won’t execute properly