Recursive Fibonacci Definition

The Fibonacci series can be defined recursively as follows:

fibonacci(0) = 0fibonacci(1) = 1fibonacci(n) = fibonacci(n – 1) + fibonacci(n – 2)

The program of Fig. 6.28 calculates the nth Fibonacci number recursively by using function fibonacci. Fibonacci numbers tend to become large quickly, although slower than factorials do. Therefore, we chose the data type unsigned long for the parameter type and the return type in function fibonacci. Figure 6.28 shows the execution of the program, which displays the Fibonacci values for several numbers.

 1   // Fig. 6.28: fig06_28.cpp 2   // Recursive function fibonacci. 3   #include <iostream> 4   using namespace ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.