UMBC CS 201, Fall 98
UMBC CMSC 201 & 201H Fall '98 CSEE | 201 | 201 F'98 | lectures | news | help

fib1

A recursive function for fibonacci with tracing.

fib1.c

/* File: fib.c * * A recursive function for fibonacci with tracing. */ #include <stdio.h> int Fib(int) ; main() { int n = -1; while (n < 1) { printf("Enter a positive integer: ") ; scanf("%d",&n); } Fib(n) ; } 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

% cc201 fib1.c % a.out 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'98 | lectures | news | help

Sunday, 06-Dec-1998 18:05:45 EST