• 0

[Java] Leibniz Pi Formula Program


Question

Yesterday in compsci class we were introduced to if()/else, while(), and for() statements. In theory it all seemed like the other things we've encountered, generally easy concepts to understand and implement.

However today we were assigned to make a program based on the Leibniz formula for Pi. Basically we are to make a program where the user specifies how many iterations of the formula are to be calculated and then the program outputs the result.

The tricky part is the sequence switching from (-) to (+) and the 1/3, 1/5, 1/7, etc sequence.

Anyone think they can point me out in a good direction?

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Have an integer that switches from -1 to +1 and back, so you can multiply ithe next term by it.

Somnething like: int sign = -1; for (...) { pi += nextTerm * sign; sign = - sign;}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.