# Help with summing series

• 11-17-2009, 03:56 AM
xplsivo
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!
• 11-17-2009, 03:58 AM
xplsivo
The table:
The table should resemble this:

i m(i)
---------------
1 99
2 45645
...
29 456
30 30
• 11-17-2009, 05:03 AM
pbrockway2
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.
• 11-17-2009, 06:03 AM
xplsivo
Hmmm...
This is what I have so far:

Quote:

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.
• 11-17-2009, 06:18 AM
collin389
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);
}
• 11-17-2009, 06:34 AM
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:

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);         }       }   }```
• 11-17-2009, 06:50 AM
xplsivo
:)
Quote:

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:

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?
• 11-17-2009, 06:28 PM
xcallmejudasx
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.
• 11-23-2009, 08:37 PM
collin389
Give us some hints on what your method needs to do. What are its parameters and return type?