# Program to make a table to compute pi

Page 1 of 1

## 4 Replies - 2516 Views - Last Post: 11 October 2012 - 02:55 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=295006&amp;s=1168c53c71eecd49b2658443c75584a5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Brovahkiin501

Reputation: 0
• Posts: 48
• Joined: 01-October 12

# Program to make a table to compute pi

Posted 09 October 2012 - 10:45 PM

Hi everyone!

I have to write a code to do the following:

Write a method to compute the following series:
m(i) = 4(1-1/3+1/5-1/7+1/9-1/11+...+1/(2i-1)-1/(2i+1)
Write a test program that displays the following table:
i m(i)
10 3.04184
20 3.09162
...
100 3.13159

This is my code so far, but i keep outputting only 0's /: If anybody can help me, it would be much appreciated!
```public class Practice {
public static void main(String[] args) {
int i = 0;
System.out.println("i \t m(i)");
while (i <= 100) {
double seriesNum = series(i);
System.out.print(i + "\t" + seriesNum + "\n");
i += 10;
}
}
public static double series(double j) {
double number = 0;
while (j % 10 != 0) {
number = 4 * (1 / (2 * j - 1) - 1 / (2 * j + 1));
j += 1;
}
return number;
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Program to make a table to compute pi

### #2 CasiOo

• D.I.C Lover

Reputation: 1524
• Posts: 3,401
• Joined: 05-April 11

## Re: Program to make a table to compute pi

Posted 10 October 2012 - 12:15 AM

```while (j % 10 != 0)

```

These are the values you use for j
0
10
20
30
...

When will while (j % 10 != 0) be true using the values above?

### #3 baavgai

• Dreaming Coder

Reputation: 6604
• Posts: 13,945
• Joined: 16-October 07

## Re: Program to make a table to compute pi

Posted 10 October 2012 - 04:02 AM

You're probably missing the point here. You do the series in the loop, printing out how you're doing with the steps. e.g.
```for(int i=2; i<=100; i++) {
// some code here
value += // your code here
if (i % 10 == 0) {
System.out.println(i + "\t" + (4.0 * value));
}
}

```

Your function for the series is wrong. Think:
```i=1, value += -1/3.0
i=2, value += 1/5.0
i=3, value += -1/7.0

```

Note the .0. If you do math with integers, you get integers. If you throw in a float, you get float.

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

## Re: Program to make a table to compute pi

Posted 10 October 2012 - 09:27 AM

number = 4 * ( 1 / (2 * j - 1) - 1 / (2 * j + 1));
1 divided by anything bigger than 1 gives 0
1.0 is different

### #5 Brovahkiin501

Reputation: 0
• Posts: 48
• Joined: 01-October 12

## Re: Program to make a table to compute pi

Posted 11 October 2012 - 02:55 PM

Hi guys,

I'm still having trouble figuring this out /: I understand that my while statement is wrong because
``` (j & 10 != 0)
```
is incorrect because the remained of my i value divided by 10 will always be 0. But if somebody could point me in the right direction of what my while statement is supposed to look like, could you help me out please? /: It would be much appreciated!