These sort of problems are actually surprisingly non-trivial due to issues with working with floating point, and decimal types for that matter.
Accepting that you want a loop solution for this (a closed form solution for n
terms does exist), first note that your series can be written as
1 + 1/5(1 + 1/2 + 1/3 + ... + 1/60)
Then note that a good rule of thumb when working with floating point types is to add the small terms first.
So an algorithm would be of the form
double sum = 0.0;
for (int i = 60; i >= 1; --i){
sum += 1.0 / i;
}
sum = sum / 5 + 1;
Note the 1.0
in the numerator; that's there to defeat integer division.
Reference: Is floating point math broken?