Home » C Tutorial » Fibonacci Series in C with Examples

Fibonacci Series in C with Examples

The Fibonacci series is nothing but the next number is the addition of the previous two numbers. Fibonacci series starts with 0 and 1 and then goes to infinity. The concept of recurrence is used to find the Fibonacci series in c. So what is the logic behind this? Let’s have a look.

The logic behind the Fibonacci series.

As mentioned above, the first two terms of the Fibonacci series are 0 and 1. The Addition of the first two terms is 1. So 1 becomes the third term in the sequence. Now the addition of the second and third term is 2. 

In general,

Fn = Fn-1 + Fn-2 

Where n should be greater than 1.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,… these are the first 15 terms of Fibonacci series. Every term is the addition of the previous two terms. 

Examples of the Fibonacci series program

There are various ways to implement the Fibonacci series logic in c programs. We can implement it by using for loop, while loop, or using the function to execute it multiple times. Also, we can print the Fibonacci series using recursion in c. Let’s understand the given examples in detail.

Fibonacci series in c using for loop

Simple logic for implementing the Fibonacci series in the c program. We have used the for loop for it and asking users for the number of terms to print.

Example

#include <stdio.h>
main()
{
 	int a=0,b=1,c;
	int i, digit;    
 	printf("Enter the number of elements:");// accepting input from user
     scanf("%d",&digit);    
 	printf("\n%d %d",a,b);//First two terms are 0 and 1    
 	for(i=2;i<digit;++i)//for loop starts from 2 because 0 and 1 are already printed    
 	{    
  		c=a+b;    
  		printf(" %d",c);    
  		a=b;// logic of fibonacci series 
  		b=c;    
 	}  
     printf("\nThese are the first %d terms of fibonacci series",digit);
}

Output:

Enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
These are first 15 terms of fibonacci series

In the above example, we have accepted the number of digits to be printed of a Fibonacci series from the user. We’ll print the first two terms of it and then we implement the logic of the Fibonacci series to print the rest of the term.

Fibonacci series in c using while loop

We are going to use a while loop in this program. Same as the above example, as a user for the number of terms to be printed.

Example

#include <stdio.h>

main()
{
 	int a=0,b=1,c;
	int i=2, digit;    
 	printf("Enter the number of elements:"); // accepting input from user
 	scanf("%d",&digit);    
 	printf("\n%d %d",a,b);//printing 0 and 1    
 	while(i<digit)// while loop for printing exact same number of terms that user entered 
 	{    
  		c=a+b;    
  		printf(" %d",c);    
  		a=b;    
  		b=c;    
  		i++;
     }  
     printf("\nThese are the first %d terms of fibonacci series",digit);
}

Output

Enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
These are first 15 terms of fibonacci series

Logic and the flow of code is the same as in the first example. Here we used a while loop to implement Fibonacci series logic.

C program for Fibonacci series using a function

We have written a function to use it multiple times in the program.

Example

#include <stdio.h>
fib(int n)
{
	int a=0,b=1,c;
	int i;
	printf("\n%d %d",a,b);//printing 0 and 1 because first two terms are always 1 and 2
 	for(i=2;i<n;++i)//for loop starts from 2 because 0 and 1 are already printed    
 	{    
  		c=a+b;    
  		printf(" %d",c);    
  		a=b;    
  		b=c;    
 }  
}

main()
{
	int digit;    
 	printf("Enter the number of elements:"); // accepting input from user   
 	scanf("%d",&digit);    
 	fib(digit);
 	printf("\nThese are the first %d terms of fibonacci series",digit);
}

Output

Enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
These are first 15 terms of fibonacci series

In the above example, we have written a function of the Fibonacci series to use it multiple times in the code.

Program for Fibonacci series in c using recursion 

In this program, we will write a function and call it recursively to get the Fibonacci series terms.

Example

#include <stdio.h>
int a=0,b=1,c; //declaring global variables
fib(int n)
{
	if(n>0) //condition for number digits in the sequence
	{
  		c=a+b;    
  		printf(" %d",c);    
  		a=b;    //logic for the fibonacci series
  		b=c;    
  		fib(n-1); //recursive call to the function
 	}  
}

main()
{
	int digit;    
 	printf("Enter the number of elements:"); // accepting input from user
     scanf("%d",&digit); 
	printf("\n%d %d",a,b);//printing 0 and 1 because first two terms are always 1 and 2   
 	fib(digit-2); // call to the function
 	printf("\nThese are the first %d terms of fibonacci series",digit);
}

Output

Enter the number of elements:20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
These are first 20 terms of fibonacci series

Instead of using a loop, the concept of recursion is used in the above example. We check the number entered as an input if it is less than zero the compiler will not execute the function. If it is greater than zero then the next term is calculated and the function call itself by decreasing input by one.

Do you know?
1. Fibonacci Series In C
2. Bubble Sort In C
3. Storage Classes in C
4. Array in C Programming

Pin It on Pinterest