C Practice Example 24

100 Classic C 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 grasp the pattern of change in the numerator and denominator.

Program source code:

Example

// Created by www.example.com on 15/11/9. // Copyright © 2015 Example. 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

100 Classic C Examples

other extensions