while (i <= n) { sum = sum + 1/(2.0*i); i = i + 1; }
Notice the use of the floating-point constant 2.0 instead of the integer constant 2 in the denominator. We do this so that 2.0*n will also be a floating-point number, which means that the division will produce a floating-point result.