In this article, i have explained about what is fibonacci in C and how to create program for fibonacci series in C, with and without recursion. Let's understand about it and create it's program in C.

Fibonacci series is a series of natural numbers where next number is equivalent to the sum of previous two numbers i.e. fn = fn-1 + fn-2.In fibonacci sequence each item is the sum of the previous two. So, you wrote a recursive algorithm, for example, recursive function example for up to 5

``````fibonacci(5) = fibonacci(4) + fibonacci(3)

fibonacci(3) = fibonacci(2) + fibonacci(1)

fibonacci(4) = fibonacci(3) + fibonacci(2)

fibonacci(2) = fibonacci(1) + fibonacci(0)``````

The first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, and each subsequent number is the sum of the previous two numbers.

First Term = 0
Second term = 1
Third Term = First + Second = 0+1 =1
Fourth term = Second + Third =1+1 = 2
Fifth Term = Third + Fourth = 2+1 = 3 and so on...

So for example fibonacci series upto 10 numbers will look like

``1 1 2 3 5 8 13 21 34 55``

Let's take a look at it's program.

### Program for Fibonacci in C

In the below code, we are using `while` loop to add the last 2 numbers and print the result until 'nth' number.

``````#include <stdio.h>

int main()
{
int i, n, firstTerm=0, secondTerm=1, sum=0;
printf("\nEnter number of terms required in Fibonacci Series: ");
scanf("%d",&n);

// Showing the first two term of the Fibonacci Series
printf("\nFibonacci Series is:\n\n\n %d %d ", firstTerm, secondTerm);
//start i =2
i=2;
//i starts from 2, as the first two terms of the series have already been shown
while (i<n)
{
sum=firstTerm+secondTerm;
firstTerm=secondTerm;
secondTerm=sum;
++i;
printf("%d ",sum);
}
return 0;
}
``````

Output:

``````Enter number of terms required in Fibonacci Series: 8

Fibonacci Series is:

0 1 1 2 3 5 8 13
``````

### Fibonacci program in C using recursion

In the Recursive way to print the Fibonacci series, we will keep calling same function 'Fibonacci' until nth number and print the sum of last 2 numbers.

``````#include<stdio.h>

int Fibonacci(int);

int main()
{
int n, i = 0, c;

scanf("%d",&n);

printf("Fibonacci series\n");

for ( c = 1 ; c <= n ; c++ )
{
//function is called recursively using for loop
printf("%d\n", Fibonacci(i));
i++;
}

return 0;
}
//fibonacci series function
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
} ``````

Output:

``````Enter number of terms required in Fibonacci Series: 8
Fibonacci series
0 1 1 2 3 5 8 13
``````

Recursion method is less efficient as it involves function calls which uses stack, also there are chances of stack overflow if function is called frequently for calculating larger Fibonacci numbers.

You may also like

Program for Matrix multiplication in C (With & Without pointers)

Creating palindrome program in C (checking string & number)

Fibonacci series In C# (Various possible ways)
Fibonacci series program in Java