Here is a row of fractions: 2/1, 3/2, 5/3, 8/5, 13/8… Each new top is the last top plus the last bottom, and each new bottom is the last top. Write series_sum(n) to add up the first n terms. The first 2 terms: 2 + 1.5 = 3.5. If n is 0, the sum is 0. In the starter, a is the top and b is the bottom.
series_sum(1)→2.0series_sum(2)→3.5series_sum(20)→32.6602607986416double series_sum(int n)double series_sum(int n) {
int a = 2, b = 1;
double sum = 0;
for (int i = 0; i < n; i++) {
// your code here: add this term a/b to sum (as a decimal)
int next = a + b;
// your code here: use next to move to the next top and bottom
}
return sum;
}
Run tries the examples. When they look right, Submit.