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 <iostream>

using namespace std;

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

    // take input of the no of terms
    cout << "Enter the no of terms = ";
    cin >> n;

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

    cout << "First " << n << " fibonacci numbers: ";

    // display first two fibonacci
    if (n >= 2)
        cout << a << " " << b;
    // display only first fibonacci
    else if (n == 1)
        cout << a;

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

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

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

Output


Enter the no of terms = 6

First 6 fibonacci numbers: 0, 1, 1, 2, 3, 5





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 <iostream>

using namespace std;

// recursive function to display
// fibonacci sequence
void fibonacci(int a, int b, int count) {
    if (count == 1) // exit condition
        cout << a << " "; // print current term
    else if (count > 1) {
         cout << 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
    cout << "Enter the no of terms = ";
    cin >> n;

    // display n no of fibonacci
    cout << "First " << n << " fibonacci numbers: ";
    // call the function
    // pass value of the first two 
    // term and total no of fibonacci
    fibonacci(0, 1, n);
    
    return 0;
}

Output


Enter the no of terms = 8

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