Categories: C++

Find Fibonacci Series

The Fibonacci Series generates the next number by adding two preceding numbers. The Fibonacci sequence begins with two numbers, F0 and F1. F0 and F1 can have initial values of 0 and 1, respectively.

For instance,

The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21

Program Code:

#include<iostream>    
using namespace std;      

void Fibonacci(int n){    
    static int n1=0, n2=1, n3;    
    if(n>0){    
         n3 = n1 + n2;    
         n1 = n2;    
         n2 = n3;    
         cout<<n3<<" ";    
         Fibonacci(n-1);    
    }    
}    
int main(){    
    int n;    
    cout<<"Enter the number of elements: ";    
    cin>>n;    
    cout<<"Fibonacci Series: ";    
    cout<<"0 "<<"1 ";  
    Fibonacci(n-2);  //n-2 because 2 numbers are already printed    
     return 0;  
}  

Output:

Enter the number of elements: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 

Note: also read about Convert Decimal to Binary

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 days ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

2 weeks ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

3 weeks ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

1 month ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

1 month ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

1 month ago