Fibonacci Series in C++

Fibonacci Series is a sequence of integers which is the sum of preceding two integers starting from 0 and 1.

Source code :

#include <iostream>
using namespace std;

int main(){
    
int limit,x,y,n;
x=-1;
y=1;
    cout<<"Enter the limit:\n";
    cin>>limit;
    cout<<"The series:\n";
    for (int i=0;i<lim;i++){
    n=x+y;
    x=y;
    y=n;
    cout<<n<<"\n";
		
    }

    return 0;
}

Output :

Enter the limit:
6
The series:
0
1
1
2
3
5

Notes :

Here the limit specifies the range to generate the series.

Algorithm :

  1. Get the limit from the user.
  2. Print first two numbers as 0 and 1 using any logic (here I used -1 and 1).
  3. Add the successive two numbers to generate the series until the limit exceeds.

Output :


                

Comments :