Easy Tutorial
❮ C Exercise Example86 C Program Structure ❯

C Exercise Example 24

C Language Classic 100 Examples

Title: There is a sequence of fractions: 2/1, 3/2, 5/3, 8/5, 13/8, 21/13... Find the sum of the first 20 terms of this sequence.

Program Analysis: Please observe the pattern of the numerator and the denominator.

Program Source Code:

Example

//  Created by www.tutorialpro.org on 15/11/9.
//  Copyright © 2015 tutorialpro.org. All rights reserved.
//

#include <stdio.h>

int main()
{
    int i, t;
    float sum = 0;
    float a = 2, b = 1;
    for (i = 1; i <= 20; i++)
    {
        sum = sum + a / b;
        t = a;
        a = a + b;
        b = t;
    }
    printf("%9.6f\n", sum);
}

The output of the above example is:

32.660263

C Language Classic 100 Examples

❮ C Exercise Example86 C Program Structure ❯