Fibonacci Series in C++ - Next Digit is equal to the sum of previous two digits.
Sounds Interesting ?
Nope
Okay, Let’s make it interesting
1, 1, 2, 3, 5, 8, 13, 21,…..
Looks Great ?
Nah !
Okay..
2=1+1
3=2+1
5=3+2
8=5+3
Now its clear enough, Let’s see a program to implement Fibonacci series in C++
· #include <iostream>
· using namespace std;
· int main() {
· int n1=0,n2=1,n3,i,number;
· cout<<"Enter the number of elements: ";
· cin>>number;
· cout<<n1<<" "<<n2<<" "; //printing 0 and 1
· for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed
· {
· n3=n1+n2;
· cout<<n3<<" ";
· n1=n2;
· n2=n3;
· }
· return 0;
· }
Output –
Enter the number of elements: 10
0 1 1 2 3 5 8 13 21 34
For Video Tutorials Subscribe our Youtube Channel
---------------------------------------------------------------------------------------------------------------------------
Previous Page Next Page
Any Doubt ? Feel free to write a comment
Join us on LinkedIn – Great Place of Networking
Follow us on More Together – Facebook for regular updates
Mandatory to join Telegram group for regular job updates
Mandatory to join whatsapp group for regular job updates
Other C++ Programs
For Other Tutorials – Click Here