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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment