login

Fibonacci Series C Program

What is Fibonacci Numbers

By definition in mathematics, the Fibonacci Numbers are the numbers in the below sequence:

 

0,\;1,\;1,\;2,\;3,\;5,\;8,\;13,\;21,\;34,\;55,\;89,\;144,\; \ldots.

 

By definition, the first Fibonacci number is 0 and second Fibonacci number is 1. Then each subsequent number is calculated based on sum of the previous two. Follow on this definition, the Fibonacci number Nth can be calculated as the formular follows:

 

Fibonacci Formula

 

with seed values

Fibonacci

In Mathematics, this is also called recurrence relation.

Recursive Fibonacci Series C function

int Fibonacci(int n)
{
     if ( n == 0 ) 
        return 0;
     if ( n == 1 ) 
        return 1;

     return Fibonacci(n-1) + Fibonacci(n-2);
}

Fibonacci Series C function using Iterative Method

 

int Fibonacci(int n)
{
   int f1 = 0;
   int f2 = 1;
   int fn;

   for ( int i = 2; i < n; i++ )
   {
      fn = f1 + f2;
      f1 = f2;
      f2 = fn;
   }
}