Fibonacci Series in C Program

  • by

Fibonacci Series in c language: This is an example of how to display Fibonacci series in C Programming for n number of terms (entered by the user) using –

1. Algorithm (the logic of the program)
2. While loop
3. For loop

In the last example, we will learn to generate c Fibonacci Series or Sequence code up to a certain number for better understanding.
All the c Fibonacci series programs are explained with proper comments and output.

What is the Fibonacci Series?

The Fibonacci Series or Sequence is a series of numbers in which first and second terms are 0 and 1 respectively. And the next term ,i.e., 3rd term, 4th term, 5th term and so on (up to nth term), is the sum of the previous two terms.
Fibonacci series is also called Fibonacci Sequence.

Fibonacci Series is :
0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21… and so on up to n terms

1st term = 0 (fixed)
2nd term = 1 (fixed)
3rd term = 1st term + 2nd term = 0 + 1 = 1
4th term = 2nd term + 3rd term = 1 + 2 = 3
5th term = 3rd term + 4th term = 2 + 3 = 5

In this way nth term will be
Fn = Fn-1 + Fn-2
with f0 = 0 and f1 = 1

F6 = 0, 1, 1, 2, 3, 5

fibonacci series in c programming
Visit the page Fibonacci Sequence, to learn more about the series.

You should have the knowledge of following C Programming topics to understand the c Fibonacci Series program better –

  • C operators
  • C for loop
  • C while and do…while loop
  • C Functions and Recursion

Fibonacci Series C Program Algorithm

An algorithm is the logic of the program. With algorithm, it becomes easy to understand any program.
Here, is the algorithm for Fibonacci Sequence C Program.

  1. First, we will take two variables: a=0 and b=1
  2. Input the number of terms from the user and store it in variable n
  3. Then we will run for loop or while loop in c to print the Fibonacci series in c up to n terms (entered by the user)
  4. And store the sum of the previous two terms in the variable sum ,i.e., sum=a+b;
START
   Step 1 → Take integer variable A, B, SUM, I, N
   Step 2 → Read N
   Step 3 → Set A = 0, B = 1, I = 1
   Step 4 → DISPLAY A
   Step 5 → REPEAT STEPS 5 TO 10 WHILE I

Fibonacci Series in C using While Loop


/* Fibonacci Series in C Programming using While Loop */

#include <stdio.h> //printf() & scanf() Header File     
#include <conio.h> //clrscr() & getch() Header File    
     
int main()
{
    int a, b, sum, i, n; 
  
    clrscr(); //to clear output screen

    printf("Enter the number of terms: "); 
    scanf("%d", &n); 
    
    a = 0;
    b = i = 1;    

    printf("Fibonacci Series in c: %d, ", a);

    while (i<n)
    {
        /* print fibonacci series in c */
        printf("%d, ", b);  
        sum = a + b;
        a = b;
        b = sum;
        i++;
    } 
getch(); //to hold output screen
return(0); }

Output –

Enter the number of terms: 10
Fibonacci Series in c: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 

Fibonacci Series C Program Explanation

Fibonacci series logic c: This is the Fibonacci Series in c program evaluation by entering the values. This explains how the values are taken in loop and how Fibonacci series in printed.

Let the number entered by the user is 5, i.e., n=5 and a=0, b=i=1
Print a ,i,e, “Fibonacci series: 0”

while (i<n)    /* while loop is executed till i is less than n */
while (i<5)   /* while loop is executed till i is less than 5 */

1st Iteration: while (1 < 5) /* condition is true */
print b ,i.e., “Fibonacci series: 0, 1”
sum = a + b = 0 + 1 = 1
a = b ,i.e., a=1
b = sum ,i.e., b=1
while loop will continue
i is incremented to 2 ,i.e., i=2

2nd Iteration: while (2 < 5)  /* condition is true */
print b ,i.e., “Fibonacci series: 0, 1, 1”
sum = a + b = 1 + 1 = 2
a = b ,i.e., a=1
b = sum ,i.e., b=2
while loop will continue
i is incremented to 3 ,i.e., i=3

3rd Iteration: while (3 < 5)  /* condition is true */
print b ,i.e., “Fibonacci series: 0, 1, 1, 2”
sum = a + b = 1 + 2 = 3
a = b ,i.e., a=2
b = sum ,i.e., b=3
while loop will continue
i is incremented to 4 ,i.e., i=4

4th Iteration: while (4 < 5)  /* condition is true */
print b ,i.e., “Fibonacci series: 0, 1, 1, 2, 3”
sum = a + b = 2 + 3 = 5
a = b ,i.e., a=3
b = sum ,i.e., b=5
while loop will continue
i is incremented to 5 ,i.e., i=5

5th Iteration: while (5 < 5)  /* condition is false */
while loop will break and displays the final output

In this way Fibonacci series c program will work and print series

Fibonacci Series in C using For Loop

To write Fibonacci in c using For loop: While Loop is replaced with For Loop with same condition


/* Fibonacci Series in C Programming using For Loop */

#include <stdio.h> //printf() & scanf() Header File    
#include <conio.h> //clrscr() & getch() Header File     

int main()
{
    int a, b, sum, i, n; 
  
    clrscr(); //to clear output screen

    printf("Enter the number of terms: "); 
    scanf("%d", &n); 
    
    a = 0;
    b = i = 1;    

    printf("Fibonacci Series in c: %d, ", a);

    for (i=1; i<n, i++)
    {
        /* print fibonacci series in c */
        printf("%d, ", b);  
        sum = a + b;
        a = b;
        b = sum;
    } 
getch(); //to hold output screen return(0); }

Output –

Enter the number of terms: 10
Fibonacci Series in c: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 

Fibonacci Series in c up to a Certain Number

In this Fibonacci series program, we print fibonacci in c up to a certain fixed number. We do not take number from the user.


/* Fibonacci Series C Program up to a Certain Number */

#include <stdio.h> //printf() & scanf() Header File      
#include <conio.h> //clrscr() & getch() Header File     

int main()
{
    int a, b, sum, i, n; 
  
    clrscr(); // to clear output screen
    
    a = 0;
    b = i = 1;  
    n = 10;   

    printf("Fibonacci Series in c is : %d ,", a);

    while (i<n)
    {
        /* print fibonacci series in c */
        printf("%d, ", b);  
        sum = a + b;
        a = b;
        b = sum;
        i++;
    }
  
    getch(); //to hold output screen
    return(0);          
}

Output –

Fibonacci Series in c is : 0, 1, 1, 2, 3, 5, 8, 13, 

Fibonacci Series in c using User-Defined Function

This c program for fibonacci series uses user-defined function fibonacci_series() to print the series. For Loop or While Loop code is written in this function.


/* Fibonacci Series c program using User-Defined Function*/

#include <stdio.h> //printf() & scanf() Header File    
#include <conio.h> //clrscr() & getch() Header File  

/* function declaration */
void fibonacci_series(int);   

int main()
{
    int n; 
  
    clrscr(); // to clear output screen

    printf("Enter the number of terms: "); 
    scanf("%d", &n);

    /* calling function */
    fibonacci_series(n); 

    getch(); //to hold output screen
    return(0);  
} 
 
void fibonacci_series(int n) 
{ 
    int a, b, sum, i;  
    a = 0;
    b = i = 1;    

    /* print Fibonacci Series in c */
    printf("Fibonacci Series in c: %d, ", a);

    for (i=1; i<n, i++)
    {
        /* print fibonacci numbers */
        printf("%d, ", b);  
        sum = a + b;
        a = b;
        b = sum;
    }  
}
Output –
Enter the number of terms: 10
Fibonacci Series in c : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 

Fibonacci Series in c using Recursion


/* Fibonacci Series c program using Recursion*/

#include <stdio.h> //printf() & scanf() Header File    
#include <conio.h> //clrscr() & getch() Header File     

int fibonacci(int n)
{
    if ((n==1)||(n==0))
    {
         return(n);
    }
    else
    {
        return(fibonacci(n-1)+fibonacci(n-2));
    }
}

int main()
{
    int n, i=1, a=0;
  
    clrscr(); // to clear output screen

    printf("Enter the number of terms: "); 
    scanf("%d", &n);

    /* print Fibonacci Series in c */
    printf("Fibonacci Series in c : %d, ", a);

    while (i<n)
    {
       printf("%d, ", fibonacci(i));
       i++; 
    }

    getch(); //to hold output screen
    return(0);  
} 

Output –

Enter the number of terms: 10
Fibonacci Series in c : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 
Note: Recursion is a function which calls itself in the definition.
Try to avoid the Fibonacci Series c program using Recursion as the Recursion method is less efficient. In this number of function calling increases due to which there are chances of stack overflow. And also it is slower than other methods (takes some time to display output due to function calling many times). So, avoid Fibonacci Series c program using recursion.
Best is to use for loop or while loop for Fibonacci series c program.