Page 173 - DCAP407_DATA_STRUCTURE
P. 173

Data Structure



                          9.3.2   Fibonacci Series
                          Fibonacci series is a series of numbers represented as 0, 1, 1, 2, 3, 5, 8, 13, 21, …. Fibonacci series starts
                          with 0 and 1. Each of the subsequent Fibonacci number is computed as the sum of the previous two
                          Fibonacci numbers.



                          Did you know?   The ratio of consecutive Fibonacci numbers is equal to 1.618. This repeatedly occurs in
                                        nature. This number has been termed as golden ratio or golden mean. Architects often
                                        design buildings,  rooms and windows whose  width and  length are in  ratio of  the
                                        golden mean.
                          Fibonacci series is defined recursively as:
                          fibonacci (0) = 0
                          fibonacci (1) = 1

                          fibonacci (n) = fibonacci (n-1) + fibonacci (n-2)
                          Let us calculate recursively the n  Fibonacci number using Fibonacci () function.
                                                    th
                                           Program to calculate recursively the n  fibonacci number using
                                                                          th
                                           fibonacci () function.
                                           /* Recursive fibonanacci function */

                                           /* A preprocessor directive to include standard input and output operations */
                                           # include <stdio.h>
                                           /* Globally declare the fibonacci function */
                                           long fibonacci (long n);
                                           /* Main function */
                                           int main (void)
                                           {

                                           /*Declare a long variable named result */
                                           long result;
                                           /*Declare a long variable named number */
                                           long number;
                                           printf (“Input an integer: “);
                                           /* Accept a long variable number */

                                           scanf (“%d”, & number);
                                           /* Fibonacci function is called and its value is stored in a variable named result */
                                           result = fibonacci (number);
                                           /* Print the value of result */
                                           printf (“Fibonacci (%1d) = %ld\n”, number, result);
                                           /* Return 0 */
                                           return 0;
                                           }





                          166                     LOVELY PROFESSIONAL UNIVERSITY
   168   169   170   171   172   173   174   175   176   177   178