0
What is wrong in this code? The Output is showing :- -Infinity Please help me and 1 more thing, I am coding it in Android?
public class Program { public static void main(String[] args) { //Alternate Series Sum Problem int n = 7; float result = 0; for(float i = 0; i <= n ; i++) if(i % 2 == 0) { result -= 1/i; } else { result += 1/i; } System.out.println(result); } }
2 Antworten
+ 6
When i=0 the output is -1/0 = -Infinity. Maybe you should start from i=1.
+ 5
Yeah, as @dimitris kapoulas pointed out that's fine because you are actually dividing by zero.
Just make sure you don't (if you were using an Integer index it would have thrown an error).
Take a look here: https://code.sololearn.com/c6F968I7jy93