write a method that takes an int k argument and returns as d
write a method that takes an int k argument and returns as double the sum of first k terms of the series: 1+ (1/2) - (1/3) + (1/4) - (1/5) ...................... using java
Solution
you can just use this without complicating the code-
public static double sum(int k)
{
 if (k<=1)
 return (double) k;
 return ((k%2==0)? 1 : -1)*(1/(double)k) + sum(k-1);
 }
Here,for k=6
sum(6)
 1/6 + sum(5)
 sum(5)
 -1/5 + sum(4)
 sum(4)
 1/4 + sum(3)
 sum(3)
 -1/3 + sum(2)
 sum(2)
 1/2 + sum(1)
 sum(1) = 1
finally,
 sum(6) = 1 + 1/2 - 1/3 + 1/4 - 1/5 + 1/6
Thankyou....

