C Program to display Fibonacci Sequence (Using recursion)

by | Jan 26, 2023 | C, C Programs


In this program, you will learn to display the sum of the Fibonacci sequence of first n numbers.

The value of n will be entered by the user.

Fibonacci sequence always starts from 0,1 i.e., the first two members of this series remain fixed and the next member is the sum of the previous two members.

For example, 0,1,1,2,3,5,8,13………….. is a Fibonacci sequence

To understand this program, you should know about

1. Recursion in C

2. Function in C

We will use recursion in this program


int fibonacci(int x); // function declaration
int main()
int n; // n is the number of terms in the sequence
printf("Enter an integer:");

int c=0;

printf("Fibonacci series terms are\n");
for(int i=1;i<=n;i++)
printf("%d\n",fibonacci(c)); // function call
int fibonacci(int x) // function definition
if(x==1 || x==0) // either first or second term
return x;
{ // recursion procedure
return ( fibonacci(x-1) + fibonacci(x-2) );




int fibonacci(int x);

first of all we declare a function by passing ‘x’ as an argument.

Then in the ‘main’ function variable ‘n’ contains no of terms in the sequence and its value is entered by the user.

for(int i=1;i<=n;i++)

for loop begins with iterator ‘i’ that starts from 1 and goes up to n.


function ‘fibonacci is called by passing the variable ‘c’ whose initial value is 0.

Now the working of ‘fibonacci’ function begins here.

if(x==1 || x==0)

for ‘c=0’ and ‘c=1’ the function returns 0 and 1 respectively.

Thus, the sequence is 0,1

Now c will incremented to 2 and the ‘else’ part of the function will gets executed

return ( fibonacci(x-1) + fibonacci(x-2) );

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

Thus, the sequence is updated and it is 0,1,1

And this will proceed upto 9 terms.




Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.