Write a method called fractionSum that accepts an integer parameter n and returns as a double the sum of the first n terms of the sequence
In other words, the method should generate the following sequence:
You may assume that the parameter n is nonnegative.
Program:
public class Ch04Ex01
{
public static void main(String[] args)
{
System.out.println("fractionSum(1): " + fractionSum(1));
System.out.println("fractionSum(20): " + fractionSum(20));
}
public static double fractionSum(int n)
{
double fsum = 0.0;
for(int i = 1; i <= n; i++)
{
fsum += (1.0 / i);
}
return fsum;
}
}
Output:
fractionSum(1): 1.0
fractionSum(20): 3.597739657143682