C Program to Print Fibonacci Series

Fibonacci series are the numbers in the following integer sequence
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ....
the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent term is the sum of the previous two terms. In mathematical terms, the Nth term of Fibonacci numbers is defined by the recurrence relation.

The C Program to Print the Fibonacci Series is a classic example that introduces beginners to loops, variables, and basic arithmetic operations in C programming. In this article, we will explore the advantages of this program, its importance for beginners, tips for writing it effectively, important points to remember, and a conclusion highlighting its educational value.

fibonacci(N) = Nth term in fibonacci series
fibonacci(N) = fibonacci(N - 1) + fibonacci(N - 2);
whereas, fibonacci(0) = 0 and fibonacci(1) = 1

Interesting facts

  • If you take any two successive (one after the other) Fibonacci Numbers, their ratio is very close to the Golden Ratio which is approximately 1.618034.
  • The bigger the pair of Fibonacci Numbers, the closer the approximation.

Advantages of Practicing the Fibonacci Series Program for Beginners

  • Introduction to Loops : This program introduces beginners to loops, such as the for loop, in C programming. It demonstrates how to use loops to repeat a sequence of instructions.

  • Understanding of Sequences : Writing this program helps beginners understand the concept of sequences and how to generate them using programming.

  • Understanding of Variables : Implementing this program helps beginners understand the concept of variables and how to use them to store and manipulate data.

  • Mathematical Concepts : The Fibonacci series is a mathematical sequence that occurs frequently in nature and mathematics. This program provides a practical example of how mathematical concepts can be implemented in programming.

  • Preparation for Advanced Topics : The skills and concepts learned from this program prepare beginners for more advanced topics in C programming, such as working with arrays, functions, and complex algorithms. It lays the foundation for understanding more complex programming concepts.

  • Enhanced Problem-solving Skills : Implementing this program enhances problem-solving skills by challenging beginners to think about how to generate the Fibonacci series using C programming concepts. It improves their ability to analyze and solve mathematical problems.

C program to print fibonacci series till N terms

In this program we first take number of terms in fibonacci series as input from user. Then starting with 0 and 1 as first two terms of the fibonacci series we generate and print consecutive fibonacci numbers by adding last two terms using a for loop.

#include <stdio.h>

int main(){
    int terms, lastNumber=1,secondLast=0;
    int currentNumber=0,counter;
    printf("Enter number of terms in Fibonacci series: ");
    scanf("%d", &terms);
    for(counter = 0; counter < terms; counter++){
        if(counter < 2){
            currentNumber = counter;
        } else {
            currentNumber = lastNumber + secondLast;
            secondLast = lastNumber;
            lastNumber = currentNumber;
        }
        printf("%d ", currentNumber);
    }
    return 0;
}

Output
Enter number of terms in Fibonacci series: 8
0 1 1 2 3 5 8 13

C Program to generate fibonacci series till Nth term and store it in array

In this program we use an array to store fibonacci series generated till now. Every fibonacci number is equal to the sum of values in last two indexes of fibonacci array.

fibonacciArray[N] = fibonacciArray[N - 1] + fibonacciArray[N - 2];
#include <stdio.h>

int main(){
    int terms, fibonacciArray[100] = {0}, counter;
    printf("Enter number of terms in Fibonacci series: ");
    scanf("%d", &terms);

    for(counter = 0; counter < terms; counter++){
        if(counter < 2){
            fibonacciArray[counter] = counter;
        } else {
            fibonacciArray[counter] = fibonacciArray[counter - 1] 
  + fibonacciArray[counter - 2];
        }
        printf("%d ", fibonacciArray[counter]);
    }
    return 0;
}
Output
Enter number of terms in Fibonacci series: 10
0 1 1 2 3 5 8 13 21 34

C program to print fibonacci series using recursion

This program uses recursion to calculate Nth fibonacci number, fibonacci(N) returns Nth fibonacci number by recursively calling fibonacci(N - 1) and fibonacci(N - 2). fibonacci(N) function does lots of repeated work by recalculating lower terns again and again.
For example:

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

It calculates value of 3rd and 4th term of fibonacci series to get 5th term.
fibonacci(6) = fibonacci(5) + fibonacci(4);

Now, while calculating 6th term, it again calculate 5th and 4th term which we already calculated while generating 5th term.
We can solve this problem of recalculating already calculated terms by storing all previous terms in an array. This approach is called recursion with memorization(Dynamic Programming).

#include <stdio.h>

int fibonacci(int term);
int main(){
    int terms, counter;
    printf("Enter number of terms in Fibonacci series: ");
    scanf("%d", &terms);
    for(counter = 0; counter < terms; counter++){
        printf("%d ", fibonacci(counter));
    }
    return 0;
}

int fibonacci(int term){
    /* Exit condition of recursion*/
    if(term < 2)
       return term;
    return fibonacci(term -1) + fibonacci(term - 2);
}
Output
Enter number of terms in Fibonacci series: 9
0 1 1 2 3 5 8 13 21

Tips for Writing the Fibonacci Series Program in C

  • Use a Loop : Use a loop, such as a for loop, to generate the Fibonacci series.

  • Initialize Variables Properly : Always initialize variables before using them to avoid undefined behavior.

  • Handle Large Numbers : Consider using data types that can handle large numbers if you plan to generate a long Fibonacci series.

  • Efficient Algorithm : Consider using an efficient algorithm, such as the iterative method or memoization, to generate the Fibonacci series.

  • Limit the Series : Limit the series to a reasonable number of terms to avoid performance issues.

  • Test the Program : Test the program with different inputs to ensure its correctness.

Conclusion

In conclusion, the C Program to Print the Fibonacci Series is a fundamental exercise that introduces beginners to loops, variables, and basic arithmetic operations in C programming. Practicing this program is important for beginners to develop a solid understanding of these concepts, which are essential for more advanced programming tasks.

Related Topics
C Program to calculate factorial of a number
C program to check a number is palindrome or not
C program to check armstrong number
C program to calculate power of a number
C program to find hcf and lcm of two numbers
C program to check string is palindrome
C program to sort characters of a string
List of all C programs