# How do I display this in java?

1/2
1/2+2/3
1/2+2/3+3/4
1/2+2/3+3/4+4/5+5/6
I need to display this in java and store the sum of each of those new lines so that I can return it as a double. I would need to use two for loops. It also accepts a parameter of n. I was also given the equation n(n+1). I see how the equation works but I don't...
show more
1/2

1/2+2/3

1/2+2/3+3/4

1/2+2/3+3/4+4/5+5/6

I need to display this in java and store the sum of each of those new lines so that I can return it as a double. I would need to use two for loops. It also accepts a parameter of n. I was also given the equation n(n+1). I see how the equation works but I don't know how to make it appear like the example above.

Here is my code:

public static double fractionSum(int n)

{

if (n < 1) {

return -1;

}

double sum;

int calc = n+1;

for (int i = 1; i <= n; i++) {

sum = n/(n+1);

for (int j = 0; j < i; j++) {

System.out.print(n+"/"+calc);

}

System.out.println(" ");

}

return 0;

}

1/2+2/3

1/2+2/3+3/4

1/2+2/3+3/4+4/5+5/6

I need to display this in java and store the sum of each of those new lines so that I can return it as a double. I would need to use two for loops. It also accepts a parameter of n. I was also given the equation n(n+1). I see how the equation works but I don't know how to make it appear like the example above.

Here is my code:

public static double fractionSum(int n)

{

if (n < 1) {

return -1;

}

double sum;

int calc = n+1;

for (int i = 1; i <= n; i++) {

sum = n/(n+1);

for (int j = 0; j < i; j++) {

System.out.print(n+"/"+calc);

}

System.out.println(" ");

}

return 0;

}

Follow

5 answers
5

Are you sure you want to delete this answer?