+92 332 4229 857 99ProjectIdeas@Gmail.com

How to calculate fibonacci series (C++)


Fibonacci series
fibonacciSeries() calculates the fibonacci series and it takes 3 parameter:
num1 is the first starting number of the fibonacci series.
num2 is the second starting number of the fibonacci series.
len is the length of the fibonacci series.

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "iomanip"
using namespace std;

void fibonacciSeries(int num1,int num2,int len)
{
unsigned long sum;
      
 for(int n=0;n<len-1;n++)
 {
  if(n==0)
   cout<<setw(5)<<num1<<setw(5)<<num2<<setw(5);
  else
  {
   sum=num1+num2;
   cout<<sum<<setw(5);
   num1=num2;
   num2=sum;
  }
 }
}

int main()
{
       fibonacciSeries (  0 ,  1 , 10 ) ; cout<<endl;
       fibonacciSeries (  1 ,  1 , 10 ) ; cout<<endl;
       fibonacciSeries ( 10 , 11 , 10 ) ; cout<<endl;
       fibonacciSeries ( 15 , 16 , 10 ) ; cout<<endl;

       _getche();
       return 0;
}

Output
    0    1    1    2    3    5    8   13   21   34
    1    1    2    3    5    8   13   21   34   55
   10   11   21   32   53   85  138  223  361  584
   15   16   31   47   78  125  203  328  531  859

0 comments: