UMBC CS 201, Fall 05
UMBC CMSC 201 Fall '05
CSEE | 201 | 201 F'05 | lectures | news | help

fib1

A recursive function for fibonacci with tracing.

fib1.c

/* File: fib1.c * Author: Richard Chang * Date: < 12/3/97 * Modified by Sue Evans - 10/30/03 * Section: 101 * EMail: bogar@cs.umbc.edu * * A recursive function for fibonacci with tracing. */ #include <stdio.h> int Fib(int) ; int main() { int n = -1; while (n < 1) { printf("Enter a positive integer: ") ; scanf("%d",&n); } Fib(n) ; return 0; } /*********************************** * Fib recursively traces the fibonacci sequence * for the nth number in the sequence * Input: the number-place in the sequence to find * Output: the value of the nth number in the sequence * Side effect: prints tracing for finding the number ***********************************/ int Fib (int n) { int result; printf("> Fib(%d)\n", n); if (n < 2) { result = 1; } else { result = Fib(n - 1) + Fib(n - 2); } printf("< %d\n", result); return (result); }

Output

Enter a positive integer: 4 > Fib(4) > Fib(3) > Fib(2) > Fib(1) < 1 > Fib(0) < 1 < 2 > Fib(1) < 1 < 3 > Fib(2) > Fib(1) < 1 > Fib(0) < 1 < 2 < 5


CSEE | 201 | 201 F'05 | lectures | news | help

Last Modified - Tuesday, 27-Sep-2005 00:26:13 EDT