C Program to Display Fibonacci Sequence

CodingTute

Updated on:

C Program to Display Fibonacci Sequence

In this article, you will learn to generate the Fibonacci sequence of n terms in multiple ways.

Below are the two ways we discussed in this article.

  • Fibonacci Series in C using Loop
  • Fibonacci Series in C using Recursion

What is Fibonacci Series?

The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding numbers. The sequence starts with 0 and 1, and then each subsequent number is the sum of the previous two numbers.

So the Fibonacci sequence goes like this:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, … and so on.

The Fibonacci sequence has numerous applications in mathematics, science, and technology, including in finance, computer algorithms, and even in the design of some natural structures such as spiral shells and the arrangement of leaves on a stem.

Fibonacci Series in C using Loop

#include <stdio.h>

int main()
{
    int n, term1=0, term2=1, nextTerm, i;
    printf("Enter N: ");
    scanf("%d", &n);
    
    //print first two terms
    printf("Fibonacci Sequence: %d, %d",term1, term2);
    
    //print from 3rd term to nth term
    for(i=3; i<=n; i++){
        nextTerm = term1 + term2;
        term1 = term2;
        term2 = nextTerm;
        printf(", %d", nextTerm);
    }
    
    return 0;
}
Run Code in Online C Compiler

Output

Enter N: 10
Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Explanation

We know the first and second terms are 0 and 1. The next term is calculated by the sum of the previous two terms.

3rd term = 2nd term + 1st term = 1 + 0 = 1

4th term = 3rd term + 2nd term = 1 + 1 = 2

5th term = 4th term + 3rd term = 2 + 1 = 3 and process continues till nth term.

Fibonacci Series in C with Recursion

#include <stdio.h>

void fibonacci(int count);

int main() {
    int count;

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

    printf("Fibonacci series:\n");

    printf("%d %d ", 0, 1);
    fibonacci(count - 2);

    return 0;
}

void fibonacci(int count) {
    static int num1 = 0, num2 = 1, num3;
    
    if (count > 0) {
        num3 = num1 + num2;
        num1 = num2;
        num2 = num3;

        printf("%d ", num3);

        fibonacci(count - 1);
    }
}
Run Code in Online C Compiler

The main() function prompts the user to enter the number of terms they want to generate, and then calls the fibonacci() function with count - 2 as the argument, because the first two terms (0 and 1) are already printed before calling the fibonacci() function.

The fibonacci() function uses the same recursive approach to generate the Fibonacci sequence. It uses the static keyword to declare the num1, num2, and num3 variables, which allows their values to persist across function calls. The count argument determines how many more terms to generate, and the function prints the value of num3 before calling itself recursively with count - 1.

Output:

Enter the number of terms: 6
Fibonacci series:
0 1 1 2 3 5

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.

Exit mobile version