Kodewick
Start learning →
Basic←→0 / 50

Fraction series sum

#026Basic(double)a / b7 min

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.

Examples
series_sum(1)→2.0
series_sum(2)→3.5
series_sum(20)→32.6602607986416
The functiondouble series_sum(int n)
main.c
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;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.