12: std::cout << “\n\n”;
13:
14: answer = fib(n);
15:
16: std::cout << answer << “ is the “ << n;
17: std::cout << “th Fibonacci number\n”;
18: return 0;
19: }
20:
21: int fib (int n)
22: {
23: std::cout << “Processing fib(“ << n << “)... “;
24:
25: if (n < 3 )
26: {
27: std::cout << “Return 1!\n”;
28: return (1);
29: }
30: else
31: {
32: std::cout << “Call fib(“ << n-2 << “) “;
33: std::cout << “and fib(“ << n-1 << “).\n”;
34: return( fib(n-2) + fib(n-1));
35: }
36: }
Enter number to find: 6
Processing fib(6)... Call fib(4) and fib(5).
Processing fib(4)... Call fib(2) and fib(3).
Processing fib(2)... Return 1!
Processing fib(3)... Call fib(1) and fib(2).
Processing fib(1)... Return 1!
Processing fib(2)... Return 1!
Processing fib(5)... Call fib(3) and fib(4).
Processing fib(3)... Call fib(1) and fib(2).
Processing fib(1)... Return 1!
Processing fib(2)... Return 1!
Processing fib(4)... Call fib(2) and fib(3).
Processing fib(2)... Return 1!
Processing fib(3)... Call fib(1) and fib(2).
Processing fib(1)... Return 1!
Processing fib(2)... Return 1!
8 is the 6th Fibonacci number
OUTPUT
126 Day 5
LISTING5.10 continued