Open In App

Fibonacci Series Program in C

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss the Fibonacci series and the programs to print the Fibonacci series in C using recursion or loops.

What is Fibonacci Series?

The Fibonacci series is the sequence where each number is the sum of the previous two numbers of the sequence. The first two numbers of the Fibonacci series are 0 and 1 and are used to generate the Fibonacci series.

In mathematical terms, the number at the nth position can be represented by:

Fn = Fn-1 + Fn-2

with seed values, F0 = 0 and F1 = 1.

For example, Fibonacci series upto 10 terms is: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

fibonacci series

The Fibonacci programs in C that print the first n terms of the series can be coded using two methods specified below:

  • Fibonacci series using recursion
  • Fibonacci series using loops

Fibonacci Series Program in C (Recursion)

In this method, we will use a function that prints the first two terms, and the rest of the terms are then handled by the other function that makes use of a recursive technique to print the next terms of the sequence.

C




// C Program to print the Fibonacci series using recursion
#include <stdio.h>
 
// first two values
int prev1 = 1;
int prev2 = 0;
 
// recursive function to print the fibonacci series
void fib(int n)
{
    if (n < 3) {
        return;
    }
    int fn = prev1 + prev2;
    prev2 = prev1;
    prev1 = fn;
    printf("%d ", fn);
    return fib(n - 1);
}
 
// function that handles the first two terms and calls the
// recursive function
void printFib(int n)
{
    // when the number of terms is less than 1
    if (n < 1) {
        printf("Invalid number of terms\n");
    }
    // when the number of terms is 1
    else if (n == 1) {
        printf("%d ", 0);
    }
    // when the number of terms is 2
    else if (n == 2) {
        printf("%d %d", 0, 1);
    }
    // number of terms greater than 2
    else {
        printf("%d %d ", 0, 1);
        fib(n);
    }
    return;
}
 
// driver code
int main()
{
    int n = 9;
    printFib(n);
    return 0;
}


Output

0 1 1 2 3 5 8 13 21 

Complexity Analysis

Time complexity: O(n), It is because, for printing n terms, the fib() function will call itself recursively for (n – 2) times and each time it will take constant time.

Space Complexity: O(n), It is because, for each recursive call of the fib() function, a separate stack frame is created. For (n-2) calls, (n-2) stack frames are created with results in the O(n) space complexity.

Fibonacci Series Program in C (Iteration)

In this method, we use one of the C loops to iterate and print the current term. The first two terms, F1 and F2 are handled separately. After that, we use two variables to store the previous two terms and keep updating them as we move to print the next term.

C




// C Program to print the fibonacci series using iteration
// (loops)
#include <stdio.h>
 
// function to print fibonacci series
void printFib(int n)
{
    if (n < 1) {
        printf("Invalid Number of terms\n");
        return;
    }
 
    // when number of terms is greater than 0
    int prev1 = 1;
    int prev2 = 0;
 
    // for loop to print fibonacci series
    for (int i = 1; i <= n; i++) {
        if (i > 2) {
            int num = prev1 + prev2;
            prev2 = prev1;
            prev1 = num;
            printf("%d ", num);
        }
 
        // for first two terms
        if (i == 1) {
            printf("%d ", prev2);
        }
        if (i == 2) {
            printf("%d ", prev1);
        }
    }
}
 
// driver code
int main()
{
 
    int n = 9;
    printFib(n);
    return 0;
}


Output

0 1 1 2 3 5 8 13 21 

Complexity Analysis

Time Complexity: O(n), Because for n number for terms, the loop inside the printFib() function will be executed n times.
Space Complexity: O(1), Because we only used a few variables which don’t depends on the number of terms to be printed.

For more details, refer to the complete article – Program to Find and Print Nth Fibonacci Numbers



Last Updated : 29 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads