Latest web development tutorials

C Exercise Example 24

100 cases of classic C language 100 cases of classic C language

Title: There is a sequence of scores: 2 / 1,3 / 2,5 / 3,8 / 5,13 / 8,21 / 13 and the front ... calculated the number of columns 20.

Program analysis: Hold the variation of the numerator and denominator.

Source Code:

//  Created by www.w3big.com on 15/11/9.
//  Copyright © 2015年 本教程. 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 above example output is:

32.660263

100 cases of classic C language 100 cases of classic C language