Pages

Monday, 9 January 2017

Chapter 18 Exercise 6, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

18.6  (Sum series) Write a recursive method to compute the following series:
 i / (i + 1)
 Write a test program that displays m(i) for i = 1, 2, . . ., 10.


public class Exercise_06 {

    public static void main(String[] args) {

        for (int i = 1; i < 10; i++) {
            System.out.println(m(i));
        }
    }

    private static double m(int i) {
        if (i == 1)
            return 1 / 2.0;
        else
            return m(i - 1) + i / (i + 1.0);
    }
}

No comments:

Post a Comment