C practical exercise topic 24 – sum of series

Time:2020-9-29

Title:There’s a sequence of fractions: 2 / 1, 3 / 2, 5 / 3, 8 / 5, 13 / 8, 21 / 13… Find the sum of the first 20 items in this sequence.

Program analysis:Please grasp the changing law of numerator and denominator.

example:

#include 
  
 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 results of the above examples are as follows:

32.660263

Thank you for your reading, please feel with your heart! Hope to help you who love learning!! Sharing is also a kind of happiness!!! Please relay…

Click to view the original text, thank you!