Easy Tutorial
❮ C Quiz C Function Isupper ❯

C Exercise Example 11

C Language Classic 100 Examples

Title: Classical Problem (Rabbit Breeding): There is a pair of rabbits, starting from the third month after birth, each month gives birth to a pair of rabbits. The young rabbits grow to the third month and then each month give birth to a pair of rabbits. If no rabbits die, how many rabbits are there each month? (Output the first 40 months)

Program Analysis: The pattern of rabbits is a sequence 1, 1, 2, 3, 5, 8, 13, 21...., where the next month is the sum of the previous two months (starting from the third month).

Program Source Code:

//  Created by www.tutorialpro.org on 15/11/9.
//  Copyright © 2015 tutorialpro.org. 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 outputs the following results:

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

C Language Classic 100 Examples

❮ C Quiz C Function Isupper ❯