Java loop runs infinite -


i doing factorial question java loop, asks 1+1/2!+1/3!+...+1/n!, n positive, using "while" make it, code run nothing :

    public static void main(string[] args) {      double sum=0,a=1;     int n=integer.parseint(args[0]);     while(n>0){          a=a*n;         sum=sum+1.0/a;     }     system.out.print(sum);  } 

please help:)

as others have pointed out, original while loop never ends, because value of n never changes, meaning while condition true (assuming original value greater zero).

is possibly trying achieve?

public static void main(string[] args) {     double sum = 0, = 1;     int n = integer.parseint(args[0]);     ( int = 1; <= n; i++ )     {         *= i;         sum = sum + (1.0 / a);     }     system.out.print(sum); } 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -