pi program

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 24 23:02:21 PDT 2015


On Friday, 25 September 2015 at 05:50:58 UTC, Charanjit Singh 
wrote:
> import std.stdio;
> import std.math;
> void main()
>
> {
>     float sum,pi,t;
>      int n=1;
>     sum=0;
>     while (n<100 )
>     {
>     t=1/( n*n);
>     n=n+1;
>     sum=sum+t;
>
>
>    }
>     writeln("value of PI=  " , (sum*6)^^.5);
>  that is pi program as
> (pi^2)/6=   1/1+  1/4  +  1/9  +  1/16  +  1/25
> but output of my program is 2.44

t=1/( n*n); //<----
Is doing integer division so is zero for n > 1 hence sqrt(1*6) = 
2.449...

change that to a
t=1.0/( n*n);



More information about the Digitalmars-d-learn mailing list