tags:

views:

27

answers:

1

So what this problem in the book is asking me to do is to write a method with a parameter N and what it should print is

1 when I call printFractionSum(1)

and when I call printFractionSum(5) it does

1+(1/2)+(1/3)+(1/4)+(1/5)

public static void main(String[] args) {
  printfractionSums(5);
}

public static void printfractionSums(int n) {
  for (int i = 1; i <= n-1; i++) {
    System.out.print(1 "+" 1/n-i);
  }
}

That is what I have so far and I don't know how to go any further. Am I doing it right? Am I doing it wrong? I would really like to know how to do this so please tell me if you want me to reword the question.

A: 

Define a double variable before entering the for loop, and use this to accumulate the sum of all terms.

Use n and not n-1 as the limit in your for loop, so that it iterates n times.

In each iteration i, just add 1/i to your result.

Grodriguez