Skip to content
Home » Programming » C Examples » Implementation of Fibonacci Series in C and its Algorithm

Implementation of Fibonacci Series in C and its Algorithm

Implementation of Fibonacci series and its algorithm

In this post, we will be exploring about the Fibonacci series. We will learn about the theory behind the Fibonacci series and algorithm to implement Fibonacci series. And yes we will implement Fibonacci series in C.

So, what is actually a Fibonacci series?

Fibonacci series is a series in which each number is the sum of the previous two numbers. In this series, we have to choose the first two numbers; they might be 0 and 1, or 1 and 1.

Example 1: 
   For 0 as first number and 1 as second number;
   0, 1, 1, 2, 3, 5, 8, 13, 21, …, up to n terms
Example 2: 
   For 1 as both first and second numbers;
   1, 1, 2, 3, 5, 8, 13, 21, …, up to n terms

So in the Fibonacci series, for the nth term we will have to add (n-1)th and (n-2)th terms. However, it is not applicable for first and second terms. They are taken manually. Mathematically, we can represent Fibonacci series as,

We will be using 0 and 1 as the first two numbers to avoid confusion from this point. 

1st term = 0 and 2nd term = 1. Then,
3rd term = 1st term + 2nd term = 0 + 1 = 1
4th term = 2nd term + 3rd term = 1 + 1 = 2
5th term = 3rd term + 4th term = 1 + 2 = 3
6th term = 4th term + 5th term = 2 + 3 = 5
7th term = 5th term + 6th term = 3 + 5 = 8
And so on up to nth term

But what if we have only one term. We will choose the first chosen number i.e. 0 (from 0 and 1). Also, in case of two terms only, we will have 0 and 1.

For 1 term only,
   Series: 0
For 2 terms only,
   Series: 0 and 1

From above explanation, we can generate a simple formula to calculate next term.

nth term = (n -1)th term + (n - 2)th term

Algorithm to find Fibonacci Series for n terms

We will be assuming the value of first and second term as 0 and 1 respectively.

Step 1: Start.
Step 2: Initialize first_term as 0, second_term as 1 and i as 0.
Step 3: Take number of terms as input from user in variable terms.
Step 4: Repeat from step 4a to step 4e until  i < terms.
    Step 4a: if i < 1 (i.e. for first term) assign value of first_term to next_term.
    Step 4b: else if i < 2 (i.e. for second term) assign value of second_term to next_term.
    Step 4c: else for all remaining terms, 
        Step 4ci: add first_term and second term and assign result to next_term.
        Step 4cii: assign second_term to first_term.
        Step 4ciii: assign next_term to second_term.
    Step 4d: Display next_term.
    Step 4e: Increment i by 1.
Step 5: End.

Pseudo Code to find Fibonacci series for n terms,

Start,
    first_term = 0
    second_term = 1
    i = 0
    terms = Take input from user for number of terms
    Repeat until i < terms,
        if i < 1,
            next_term = first_term
        else if i < 2,
            next_term = second_term
        else,
            next_term = first_term + second_term
            first_term = second_term
            second_term = next_term
        Display next_term
        i = i + 1
End.

Program to find Fibonacci Series in C

//fibonicca series upto 10th term

#include<stdio.h>

void main() {
    int first_term = 0, second_term = 1, next_term, terms, i;
    printf("Enter the number of terms: ");
    scanf("%d", &terms);
    for (i=0; i < terms; i++) {
        // For First Term
        if (i < 1) {
            next_term = first_term;
        }
        // For Second Term
        else if (i < 2) {
            next_term = second_term;
        }
        // For Remaining Terms
        else {
            next_term = first_term + second_term;
            first_term = second_term;
            second_term = next_term;
        }
        printf("%d ", next_term);
    }
}

Note: The values of first and second terms can be chosen as per you requirements or you can take input from user for first and second term.

Output:

Let’s see output for 10 terms,

Enter the number of terms: 10
 0 1 1 2 3 5 8 13 21 34 

For 4 terms,

Enter the number of terms: 4
 0 1 1 2 

Explanation

  • At first, we have to initialize the values of first_term and second_term as 0 and 1 respectively. You can give user right to choose first and second terms by taking them as input. For now, we have provided it manually.
int first_term = 0, second_term = 1
printf("Enter the number of terms: ");
scanf("%d", &terms);
  • Then, we will repeat/loop till i < terms. Initially value of i is set to 0 and i is incremented by 1 for every loop.
for (i=0; i < terms; i++) {
    . . . 
}
  • Moving forward, we should identify if current term is first term or not. For this, we check if i < 1. If yes, we assign first_term term to next_term which will be printed as first term.
// For First Term
if (i < 1) {
    next_term = first_term;
}
  • Similarly, we should identify if the current term is second term. For this, we check if i < 2. If yes, we assign second_term to next_term.

// For Second Term
else if (i < 2) {
next_term = second_term;
}

  • Then, for all terms except first and second term we have to add first_term and second term and assign it to next_term. After that, assign second term to first term and next_term to second_term.
// For Remaining Terms
else {
     next_term = first_term + second_term;
     first_term = second_term;
     second_term = next_term;
}
  • Finally, next_term is printed which contains the value of current nth term where n = 1, 2, 3, 4,…, n.
printf("%d ", next_term);

More Examples

Leave a Reply

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