C Program to Display Fibonacci Sequence

Fibonacci sequence

C program to display fibonacci sequence has been shown here. For example if the total no terms is $10$, we get the first $10$ fibonacci numbers i.e. $0, 1, 1, 2, 3, 5, 8, 13, 21, 34$. The following section covers the iterative approach to find fibonacci sequence. The algorithm, pseudocode and time complexity of the program have been shown below.






1. Algorithm to display fibonacci sequence


1. Take number of terms n as input.

2. Intialize a counter variable say count = 0

3. Assign the first two fibonacci numbers to variables a, b i.e. a = 0 and b = 1

4. Display a and b.

5. Perform t = a + b and display t as next fibonacci.

6. Perform a = b and b = t

7. Perform count = count + 1.

8. If count < n - 2, go to step 5 else stop the process.




2. Pseudocode to display fibonacci sequence


Input : No of terms $n$

Output : First $n$ no fibonacci

1. Procedure fibonacciSequence($n$):

2. $count \leftarrow 0$

3. $a \leftarrow 0$

4. $b \leftarrow 1$

5. Display $a, b$

6. Repeat until $count < n - 2$:

7. $t \leftarrow a + b$

8. Display $t$

9. $a \leftarrow b$

10.$b \leftarrow t$

11.$count \leftarrow count + 1$

12. End Procedure





3. Time complexity to display fibonacci sequence


Time Complexity: O(n)

Where n is the total no of terms in fibonacci sequence.





4. C Program & output to display fibonacci sequence using iteration

Code has been copied
/******************************
    alphabetacoder.com
C program to display fibonacci 
sequence using iteration
*******************************/

#include <stdio.h>

int main() {
    // declare variables
    int n, count = 0, a, b, t;

    // take input of the no of terms
    printf("Enter the no of terms = ");
    scanf("%d", & n);

    // intialize the first two number of the sequence
    a = 0;
    b = 1;

    printf("First %d fibonacci numbers: ", n);

    // display first two fibonacci
    if (n >= 2)
        printf("%d, %d", a, b);
    // display only first fibonacci
    else if (n == 1)
        printf("%d", a);

    //now calculate the remaining n-2 numbers in the sequence
    while (count < n - 2) {
        // calculate next fibonacci
        t = a + b;
        //display next fibonacci
        printf(", %d", t);

        //assign values for next iteration
        a = b;
        b = t;

        // increment the counter
        count = count + 1;
    }

    return 0;
}

Output


Case 1:

Enter the no of terms = 10

First 10 fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34


Case 2:

Enter the no of terms = 2

First 2 fibonacci numbers: 0, 1


Case 3:

Enter the no of terms = 20

First 20 fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 3 77, 610, 987, 1597, 2584, 4181





5. C Program & output to display fibonacci sequence using recursion

Code has been copied
/******************************
    alphabetacoder.com
C program to display fibonacci 
sequence using recursion
*******************************/

#include <stdio.h>

// recursive function to display
// fibonacci sequence
void fibonacci(int a, int b, int count) {
    if (count == 1) // exit condition
        printf("%d ", a); // print current term
    else if (count > 1) {
        printf("%d ", a); // print current term
        // call function
        fibonacci(b, a + b, count - 1);
    }
}

int main() {
    // declare variables
    int n;

    // take input of the no of terms
    printf("Enter the no of terms = ");
    scanf("%d", & n);

    // display n no of fibonacci
    printf("First %d fibonacci numbers: ", n);
    // call the function
    // pass value of the first two 
    // term and total no of fibonacci
    fibonacci(0, 1, n);

    return 0;
}

Output


Case 1:

Enter the no of terms = 10

First 10 fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34


Case 2:

Enter the no of terms = 2

First 2 fibonacci numbers: 0, 1