Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

CPP Program to Print Fibonacci Series up to N Numbers

Last Updated on May 5, 2023 by Prepbytes

The following element in the Fibonacci sequence is equal to the sum of the preceding two components. It bears the name of the early thirteenth-century Italian mathematician Leonardo Fibonacci. Each number in the Fibonacci series equals the sum of the two numbers before it.
According to the selected beginning point for the sequence, the first two numbers in the Fibonacci sequence must be either 1 and 1 or 0 and 1, and each succeeding number must be the sum of the two before it.

What is Fibonacci Series?

Any number in the series can be calculated as the direct sum of the two numbers before it.

Following is Fibonacci Series –

0, 1, 1, 2, 3, 5, 8, 13, 21

How does the Fibonacci Sequence Function Work?

The sum of the two numbers preceding it determines the next number in the Fibonacci sequence. A Fibonacci sequence starts with the digits 0 and 1.

  • The function defines the Fibonacci number sequence Fn in mathematical terms.

    Fn=Fn-1 + Fn-2

  • Using the values of the first two terms,

    F0 = 0 and F1 = 1

  • The Fibonacci series begins as follows.

    0,1,1,2,3,5,8,13,21,……..

Methods to Write Fibonacci Series

Fibonacci series can be written in two ways –

  • Fibonacci series without recursion
  • Fibonacci series using recursion

Method 1 to Implement Fibonacci Series without Recursion ( Iterative Approach)

Without recursion, Fibonacci sequences use two variables, f1, and f2, and initialize them with 0 and 1 correspondingly as those values correspond to the first and second components of the Fibonacci sequence.
Print f2 after iterating from 1 to n-1, then save it in a temporary variable and update f2 with f2 + f1 and f1 as f2.

Code Implementation

#include <bits/stdc++.h>  
using namespace std;  

int main() 
{  
      int number = 7;
      int f1=0,f2=1,f3;
       
     cout<<f1<<" "<<f2<<" ";  
     for(int i=2;i<number;i++)     
     {    
       f3=f1+f2;    
       cout<<f3<<" ";    
       f1=f2;    
       f2=f3;    
     }
     return 0;
}

Output

0 1 1 2 3 5 8

Time Complexity : O(n)

Space Complexity : O(1)

Method 1 to Implement Fibonacci Series using Recursion

Steps to build the Fibonacci series using recursion:

Create a recursive function with an integer parameter N.

  1. The Fibonacci number will be zero if N = 0.
  2. Otherwise, if n=1 The first Fibonacci number is 1.
  3. If not, func(n-1) + func(n-2) return value.

Code Implementation

#include <bits/stdc++.h>
using namespace std;

int fibonacci_series(int n)
{
    if(n == 0){
        return 0;
    }
    else if(n == 1){
        return 1;
    }
    else{
        return fibonacci_series(n-2) + fibonacci_series(n-1);
    }
}
  
int main() {
    int n = 7;
      for(int i = 0; i < n; i++)
    {
        cout << fibonacci_series(i) << " ";
    }
    return 0;
}

Output

0 1 1 2 3 5 8

Time Complexity : O(n*2^n)

Space Complexity : O(n) ( Recursion stack space)

Conclusion
In this article, we have discussed the Fibonacci series and how the series works. Apart from it, we have also discussed the implementation of the Fibonacci series with an iterative as well as with a recursion approach.

Frequently Asked Questions

Q1. What is C++’s highest possible Fibonacci number?
Ans. In C++, the Fibonacci sequence is limited to 47 digits.

Q2. What is the Fibonacci golden rule?
Ans. By dividing each number in the Fibonacci series by its direct predecessor, the golden ratio is obtained. Mathematically speaking, the quotient F(n)/F(n-1) will become closer to the limit 1.618 for rising values of n if F(n) specifies the nth Fibonacci number. The golden ratio is a suitable name for this restriction.

Q3. What is the Fibonacci number that is 10?
Ans. Fib(10) = 55 is the tenth Fibonacci number. The index number for 55 (10th in the list of Fibonacci numbers) is 5+5 or 10, which is the total of its digits.

Q4. Is the number 0.886 a Fibonacci one?
Ans. The plot point is between 0.00 and 1.00, and the whole Fibonacci series of retracement ratios is 0.25, 0.38, 0.50, 0.618, 0.786, and 0.886.

Q5. How does a Fibonacci sequence contain the letter N?
Ans. The sum of the (n-1)th and (n-2)th Fibonacci numbers is the n-th Fibonacci number.

Leave a Reply

Your email address will not be published. Required fields are marked *