Series: 1 + 1/3 + 1/5 +...upto N terms

The problem in your code lies on this line:

c = c + (1/i);

Here, the operation performed inside the parentheses is integer division! So, when i has any value greater than 1, the result will be zero. This zero is then converted to a float value.

To force the compiler to use floating point division, use this:

c = c + (1.0/i);

I agree with Adrian's answer.

Another issue is because of the way floating point numbers are represented in a system when they are added in arbitrary order, precision can be lost.

To have maximum precision, floating point numbers should be added from smallest first to largest last.

Tags:

C

Loops

Series