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!

C Program to Print Fibonacci Series up to N Numbers

Last Updated on May 9, 2023 by Prepbytes

The sum of the first two elements in the Fibonacci sequence is equal to the next element. It is named after the Italian mathematician Leonardo Fibonacci, who was active in the early thirteenth century. The sum of the two numbers preceding it is equal to each number in the Fibonacci sequence.
The first two numbers in the Fibonacci sequence must either be 1 and 1 or 0 and 1, depending on the chosen starting point, and each consecutive 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?

The following number in the Fibonacci sequence is determined by adding the two numbers before it. The numbers 0 and 1 are the first two of a Fibonacci sequence.

The function gives a mathematical definition of the Fibonacci number series Fn.

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,........

In two ways Fibonacci series can be written –

  • Fibonacci series without recursion
  • Fibonacci series using recursion

Method 1 of the Fibonacci Series Without Recursion ( Iterative Approach)

Fibonacci sequences avoid using recursion and instead use two variables, f1, and f2, with starting values of 0 and 1, respectively, to represent 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 2 of the 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
The Fibonacci sequence and its operation are discussed in this article. In addition to that, we also talked about how the Fibonacci sequence may be implemented using both an iterative and recursive method.

Frequently Asked Questions

Q1. How do you find the Fibonacci series up to N terms?
Ans. Fibonacci Numbers The term "Fibonacci Series" refers to a group of numbers where each number is the sum of the two numbers before it. The general formula for determining the Nth term in a series is F0 = 0 and F1 = 1. Fn = Fn-1 + Fn-2 is the formula for the Nth term in a Fibonacci sequence.

Q2. What is the Nth term of Fibonacci?
Ans. The Fibonacci Sequence’s first 20 terms are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, and 4181.

Q3. What is the formula for the first N Fibonacci numbers?
Ans. The formula for Fibonacci numbers is Fn = Fn-1 + Fn-2, where Fn is the (n + 1)th term and n > 1. The first and second Fibonacci numbers are written as F0 = 0 and F1 = 1, respectively.

Leave a Reply

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