Latest web development tutorials

C Exercise Example 11

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

Title: Classical problem (rabbit born cub): a pair of rabbits from the first 3 months after birth monthly birth to a rabbit, a small rabbit to the first three months after the month long again with one pair of rabbits, if rabbits do not die, the total number of rabbits per month to ask how much? (Output of the previous 40 months can be)

Program analysis: number of rabbits column 1,1,2,3,5,8,13,21 law .... that is the next month and two months (from the first three months).

Source Code:

//  Created by www.w3big.com on 15/11/9.
//  Copyright © 2015年 本教程. All rights reserved.
//

#include<stdio.h>

int main()
{
    int f1=1,f2=1,i;
    for(i=1;i<=20;i++)
    {
        printf("%12d%12d",f1,f2);
        if(i%2==0) printf("\n");
        f1=f1+f2;
        f2=f1+f2;
    }
    
    return 0;
}

The above example output is:

           1           1           2           3
           5           8          13          21
          34          55          89         144
         233         377         610         987
        1597        2584        4181        6765
       10946       17711       28657       46368
       75025      121393      196418      317811
      514229      832040     1346269     2178309
     3524578     5702887     9227465    14930352
    24157817    39088169    63245986   102334155

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