This week's giveaway is in the Testing forum.We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# how i can write the equation for this series?

Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:
i have an assignments and i don't know how i can do it and make the equation

Create a program using a for-loop to calculate the value of pi from the infinite series:
pi = 4 – (4/3) + (4/5) – (4/7) + (4/9) – (4/11) + . . .
Allow the user to specify the number of series of computations to approximate the
values of pi.

Bartender
Posts: 4905
185
• Number of slices to send:
Optional 'thank-you' note:
hi Sara,

do you know how to ask the user for the number of terms, and next obtain that number?

Then, say the number obtained is 5. Then your denominators in the series are 1, 3, 5, 7, 9. Can you find a way to derive these denominators, given the number of terms?

Marshal
Posts: 75637
354
• 1
• Number of slices to send:
Optional 'thank-you' note:
Welcome to the Ranch

You realise that series will evaluate to 3? The reason is that all the divisions are in integer format; the first division will evaluate to 1 and the others to 0. You need to use floating‑point division:-The divisors can be denominated as integers which makes the loop easier to write; the runtime will convert them to floating‑point automatically. The poor precision of floating‑point arithmetic means you will never get an accurate value for π, but you can still get full marks.

Campbell Ritchie
Marshal
Posts: 75637
354
• Number of slices to send:
Optional 'thank-you' note:
Note the first term in the sum isn't 4 but 4 &div 1. Actually, I have only seen π's expansion written asThat is the expansion of arctan(1) multiplied by 4 because arctan(1) is π ÷ 4 (45°).

Correct / 4 to / 3.

 Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ... Free, earth friendly heat - from the CodeRanch trailboss https://www.kickstarter.com/projects/paulwheaton/free-heat