Abstract

This program tests three Fibonacci functions, with dynamic, iterative, and recursive algorithms.  The program repeatedly prompts for an integer input, and then prints the results from the three functions. The program exits when a negative number is entered.

Source Code - testfib.c

/**********************************************************
 *   This program tests three Fibonacci functions,
 ^ with dynamic, iterative, and recursive algorithms.
 *   The program repeatedly prompts for an integer input,
 * and then prints the results from the three functions.
 *   The program exits when a negative number is entered.
 *   Because the 48th Fibonacci number is larger than
 * the range of unsigned long (32 bit) numbers, all
 * functions will overflow above 47.
 *   WARNING: for i greater than about 45, the time 
 * to calculate the Fibonacci number with a recursive 
 * algorithm is quite long.
 **********************************************************/

#include "Fib.h"
#include <stdio.h>

int main () {
  int i;
  printf ("Fibonacci function test program version C 1\n");
  printf ("Enter a negative number to quit\n");
  printf ("i?  ");
  scanf ("%d", &i);
  while (i >= 0) {
    printf ("Dynamic:   %lu\n", Fib_D (i));
    printf ("Iterative: %lu\n", Fib_I (i));
    printf ("Recursive: %lu\n", Fib_R (i));
    printf ("i?  ");
    scanf ("%d", &i);
  }
  return 0;
}


Reference Links

Fibonacci Functions