0
public class Model {
    public static double reciprocalSum(int n, int m) {
        float i;
        float a;
        float s = 0;
        for (i = n; i <= m; i++) {
            a = 1 / i;
            s += a;
        }
        if (n >= m) {
            a = (float) n;
            s = a / m;
        }
        return Math.round(s * 100.0) / 100.0;
    }
    public static void main(String[] args) {
        System.out.println(reciprocalSum(5, 3));
    }
}

I have created a method to produce the sum of the reciprocals between two integers (inclusive, this part works, example: n = -5, m = -3, returns -0.78). I also wanted divide if integer "n" is greater than integer "m" (n/m). I have attempted to do so with the if statement, but it does not want to generate the decimal places. This output only shows "1.0" (as opposed to 1.67). Is there an issue due to "s" being a float and "n" & "m" being integers? Any help is appreciated.

Fixed and edited.

Markus Z
  • 17
  • 3

0 Answers0