Skip to content

Commit 9b99cf5

Browse files
author
jsquared21
committed
Add Ex 18.5
1 parent 9c99076 commit 9b99cf5

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Diff for: Exercise_18/Exercise_18_05/Exercise_18_05.class

881 Bytes
Binary file not shown.

Diff for: Exercise_18/Exercise_18_05/Exercise_18_05.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*********************************************************************************
2+
* (Sum series) Write a recursive method to compute the following series: *
3+
* *
4+
* m(i) = 1 / 3 + 2 / 5 + 3 / 7 + 4 / 9 + 5 / 11 + 6 / 13 + ... + i / (2i + 1) *
5+
* *
6+
* Write a test program that displays m(i) for i = 1, 2, . . ., 10. *
7+
*********************************************************************************/
8+
public class Exercise_18_05 {
9+
/** Main method */
10+
public static void main(String[] args) {
11+
// Display m(i) for i = 1 - 10
12+
System.out.println("\n i m(i)");
13+
System.out.println("-----------");
14+
for (int i = 1; i <= 10; i++) {
15+
System.out.printf("%2d%8.2f\n", i, m(i));
16+
}
17+
}
18+
19+
/** Method for computing m(i) */
20+
private static double m(double i) {
21+
if (i == 0) // Base case
22+
return 0;
23+
else
24+
return i / (2 * i + 1) + m(i - 1); // Recursive call
25+
}
26+
}

0 commit comments

Comments
 (0)