C++ Program to Print Fibonacci Series

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

// Program for fibonacci series
// 0 1 1 2 3 5 8 13 21
#include<iostream>
#define clrscr() system("cls")
using namespace std;

int main()
{
      int n,a,b,c,i=1;
      clrscr();
      cout<<"Enter the limit";
      cin>>n;
      a=0;
      b=1;
      if(n==1)
       cout<<a;
       else if (n==2)
       cout<<" "<<a << " "<<b;
      else if(n>2)
      {
         cout<<" "<<a << " "<<b;    
        
         while(i<=n-2) 
         {
               c=a+b;             
               cout<<" "<<c; 
               a=b;
               b=c;
               i++;
         }
      }
}

 

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

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