# Thread: Help with summing series

1. Member
Join Date
Nov 2009
Posts
4
Rep Power
0

## Help with summing series

I am trying to write a method to compute the series:

m(i) = 1/2 + 2/3 + ... + i/(i + 1)

I am trying to display them in a table but I just can't get it to come out right. I would prefer the table to look like the following:

i m(i)
---------------
1 99
2 45645
...
29 456
30 30

Can anyone out there help me do this? I would be much obliged!
Last edited by xplsivo; 11-17-2009 at 04:44 PM.

2. Member
Join Date
Nov 2009
Posts
4
Rep Power
0

## The table:

The table should resemble this:

i m(i)
---------------
1 99
2 45645
...
29 456
30 30
Last edited by xplsivo; 11-17-2009 at 04:44 PM.

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,717
Rep Power
16
Use a for loop to produce each row. The printf() method will allow you to print the accumulating total so that it has 4 decimal places.

As no-one is likely to actually write the code for you, you will get the best help if you post your code and describe whatever problem you have with it.

4. Member
Join Date
Nov 2009
Posts
4
Rep Power
0

## Hmmm...

This is what I have so far:

Originally Posted by xplsivo
for(int i=1; i<=20; i++){
System.out.println(i + "\t\t" + convert(i,i));
The code seems to be computing i/(i + 1) but it will not add the previous back to it.
Last edited by xplsivo; 11-17-2009 at 04:43 PM.

5. Senior Member
Join Date
Nov 2009
Posts
236
Rep Power
9
you could say:

int value = 0;
System.out.println("i - m(i)");
System.out.println("---------------------");
for( int i = 0; i < 21; i++)
{
System.out.println( i + " - " + value + "\n" );
value = (i+1)/(i+2);
}

6. Senior Member
Join Date
Nov 2009
Posts
236
Rep Power
9
Oh, and something to notice, then you divide int you wont get a double you have to divide them as doubles. Also i forgot the +=, it should be:
value += ((double) i + 1)/(i+2);
the second one is automatically casted as a double.
here is the exact code, tested:

Java Code:
```    public class series
{
public static void main( String args[] )
{
double value = 0;
System.out.println("i - m(i)");
System.out.println("---------------------");
for( int i=0; i < 21; i++)
{
System.out.println( i + " - " + value + "\n" );
value += ((double) i + 1)/(i+2);
}
}
}```

7. Member
Join Date
Nov 2009
Posts
4
Rep Power
0

## :)

Originally Posted by collin389
Oh, and something to notice, then you divide int you wont get a double you have to divide them as doubles. Also i forgot the +=, it should be:
value += ((double) i + 1)/(i+2);
the second one is automatically casted as a double.
here is the exact code, tested:

Java Code:
```    public class series
{
public static void main( String args[] )
{
double value = 0;
System.out.println("i - m(i)");
System.out.println("---------------------");
for( int i=0; i < 21; i++)
{
System.out.println( i + " - " + value + "\n" );
value += ((double) i + 1)/(i+2);
}
}
}```
This rocks, thanks! It does everything I need it to do, If I could just figure out how to write it where it computes it within its own method, then I'll be good to go. Could you point me in the right direction please?

8. You mean recursively? Instead of having a for loop you return value and your counter and then have a base case to break if your counter reaches 21 or your value reaches a certain spot.

9. Senior Member
Join Date
Nov 2009
Posts
236
Rep Power
9
Give us some hints on what your method needs to do. What are its parameters and return type?