/**************************************************************************** * fs1.c * * Computer Science 50 * David J. Malan * * Computes n'th Fibonacci number. * * Demonstrates recursion (and bad design). ***************************************************************************/ #include #include #include /* prototype */ long long fs(int); int main(int argc, char * argv[]) { int n; /* ensure proper usage */ if (argc != 2) { printf("Usage: %s n\n", argv[0]); return 1; } /* compute and print n'th number in Fibonacci sequence */ n = atoi(argv[1]); if (n < 0) printf("Input must be non-negative.\n"); else printf("fs(%d) = %lld\n", n, fs(n)); } /* * long long * fs(int n) * * Returns n'th number in Fibonacci sequence. */ long long fs(int n) { /* compute n'th number */ if (n == 0) return 0; else if (n == 1) return 1; else return (fs(n-1) + fs(n-2)); }