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 August 7, 2024 by Abhishek Sharma

The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. This series is an important concept in mathematics and computer science due to its simple recursive nature and numerous applications in algorithms, data structures, and problem-solving. Writing a C++ program to print the Fibonacci series up to N numbers is a common exercise that helps in understanding loops, recursion, and efficient computation methods. This article provides an introduction to a C++ program for generating the Fibonacci series and discusses its implementation.

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
Generating the Fibonacci series up to N numbers in C++ is a fundamental exercise that reinforces understanding of loops, recursion, and efficient programming techniques. Whether using iterative methods for simplicity or recursive approaches to explore algorithmic depth, implementing the Fibonacci series in C++ offers a practical way to apply basic programming concepts. This exercise not only helps in grasping the mechanics of C++ programming but also lays the foundation for more complex problem-solving scenarios in computer science.

Frequently Asked Questions related to CPP Program to Print Fibonacci Series up to N Numbers

Here are some FAQs related to CPP Program to Print Fibonacci Series up to N Numbers:

1. What is the Fibonacci series?
The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, typically starting with 0 and 1. The sequence looks like this: 0, 1, 1, 2, 3, 5, 8, 13, and so on.

2. How can I generate the Fibonacci series up to N numbers in C++?
You can generate the Fibonacci series in C++ using either an iterative approach with loops or a recursive approach. The iterative approach is generally more efficient in terms of time and space complexity.

3. Which method is more efficient for generating the Fibonacci series, iterative or recursive?
The iterative method is more efficient for generating the Fibonacci series as it has a time complexity of O(N) and a space complexity of O(1). The recursive method, unless optimized with memoization, has a time complexity of O(2^N) due to redundant calculations.

4. What is memoization, and how does it improve the efficiency of the recursive method?
Memoization is a technique used to store the results of expensive function calls and reuse them when the same inputs occur again. In the context of the Fibonacci series, memoization can significantly reduce the time complexity of the recursive method by avoiding redundant calculations.

5. Can the Fibonacci series be generated using dynamic programming?
Yes, the Fibonacci series can be efficiently generated using dynamic programming. This approach involves storing the results of previous computations in an array and using them to compute the next numbers in the series, resulting in a time complexity of O(N) and space complexity of O(N).

6. Why is learning to generate the Fibonacci series in C++ useful?
Learning to generate the Fibonacci series in C++ is useful because it introduces fundamental programming concepts such as loops, recursion, and algorithm optimization. It also provides a practical example of how mathematical sequences can be implemented in code, which is a valuable skill in many areas of computer science and software development.

Leave a Reply

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