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:
• Tim Cooke
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Rob Spoor
• Bear Bibeault
Saloon Keepers:
• Jesse Silverman
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Carey Brown
Bartenders:
• Piet Souris
• Al Hobbs
• salvin francis

# how i can write the equation for this series?

Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:
i want to find the sum of the following series 1 + ½ + ¼ + 1/6 + . . . + 1/100
by using for loop
how i can write the formula?

i use eclipse

i try this code but the answer is 0 whish is wrong

Master Rancher
Posts: 4062
56
• 2
• Number of slices to send:
Optional 'thank-you' note:
The problem is in this line:

Although you have correctly declared "u" to be a double, Java evaluates this statement by first computing

And for that part, both 1 and n are integers.  This means Java uses integer division for this, which unfortunately means it rounds the answer down to the nearest integer value below the "correct" answer.  Which in this case, is 0.

After that, it converts the int 0 to a double 0.0.  But by then it's too late, you've lost the information that you were looking for; only 0 remains.

To fix it, either the 1 or the n needs to be made into a double before the division.  A simple way to do that is:

Another is:

You have a bit more work to get to the final answer.  Good luck...

Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:

Marshal
Posts: 74376
334
• Number of slices to send:
Optional 'thank-you' note:
Welcome to the Ranch

Good to see somebody who indents their code and uses the code button
Please don't declare two variables on the same line. Please always wrap the body of a loop in {...}. Never use floating‑point numbers as a loop variable. Always use integers because integer arithmetic is always precise. And always declare the loop variable in the loop if possible.
Your method doesn't calculate a sum, so it should be renamed.You do realise that your code calculates something different from the original post? And that you aren's using the method argument at all?

Rancher
Posts: 990
23
• Number of slices to send:
Optional 'thank-you' note:
you have a very simple progression of 1/(i*2), where i runs from 1 to 50.  gives you ever even that you have shown, but that 1 becomes a non trivial problem unless you are given the series starts a 1 and you just have to generate the fractions.  if you must generate the 1, the problem becomes a lot less trivial as a series.

Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:

Campbell Ritchie
Marshal
Posts: 74376
334
• 1
• Number of slices to send:
Optional 'thank-you' note:

Les Morgan
Rancher
Posts: 990
23
• Number of slices to send:
Optional 'thank-you' note:
lol... yes, i cannot believe none of us did that---and here it is:

you are doing integer division.  integers are truncated for any non whole number, so 1/n always = 0.  you need to either cast everything to doubles or use doubles in your calculations--at least floats, but doubles would be better.

Mike Simmons
Master Rancher
Posts: 4062
56
• Number of slices to send:
Optional 'thank-you' note:
I believe this was covered back in the second post in the thread.