4 Replies - 1590 Views - Last Post: 11 October 2012 - 02:55 PM Rate Topic: -----

#1 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • Posts: 3,166
  • Joined: 05-April 11

Re: Program to make a table to compute pi

Posted 10 October 2012 - 12:15 AM

Look at your series method

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?
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,908
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1