home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / MCC / FIBO.C < prev    next >
C/C++ Source or Header  |  1993-12-01  |  613b  |  36 lines

  1. /*
  2.  * Program to calculate a fibonacci number.
  3.  *
  4.  * This demonstrates a heavily RECURSIVE function.
  5.  *
  6.  * Copyright 1988,1990 Dave Dunfield
  7.  * All rights reserved.
  8.  */
  9. #include \mc\stdio.h
  10.  
  11. #define MAXFIB    24    /* Largest we can do in 16 bits */
  12.  
  13. /*
  14.  * Recursive function to calculate a fibonacci number
  15.  */
  16. unsigned fibo(num)
  17.     unsigned num;
  18. {
  19.     if(num <= 2)
  20.         return 1;
  21.  
  22.     return fibo(num-1) + fibo(num-2);
  23. }
  24.  
  25. /*
  26.  * Main function to call "fibo" in a loop,
  27.  * and display the result.
  28.  */
  29. main()
  30. {
  31.     int i;
  32.  
  33.     for(i=1; i <= MAXFIB; ++i)
  34.         printf("Fibonacci(%u) = %u\n", i, fibo(i));
  35. }
  36.