Java: Sum series logical error- final sum printing for all variables -
i tried write program should printing iterative sums.
but current code instead printing final sum each iteration, , can't figure why :
package sumseries; public class sumseries { public static void main(string[] args) { system.out.println("i" + "\t\t"+ "m(i)"); system.out.println("==================="); int x=0; for(int i=1;i<=20;i++){ system.out.println( + "\t\t" + series(x) ); } } public static double series(int i){ double sum=0; for(i=0;i<=20;i++){ sum += ((double) i)/(i+1); } return sum; } }
what wrong here , how can fix ?
in method :
public static double series(int i){//the parameter variable double sum=0; for(i=0;i<=20;i++){ // again initialising 0; sum += ((double) i)/(i+1); } return sum; }
i have written comments adjoining lines. same sum
whatever might pass parameter method.
Comments
Post a Comment