Pages

Ads 468x60px

Sunday, July 22, 2012

C++: Fibonacci sequence recursively


#include <iostream>

using namespace std;

int fun(int num)
{
if (num==0 || num==1)
return num;
else
return (fun(num-1)+fun(num-2));
}

void main()
{
cout<<"THE FIRST 25 NUMBERS OF A FIBONACCI SEQUENCE.\n\n\n";

for (int i=1; i<=25; i++)
{
cout<<fun(i)<<"\t";
}
cout<<"\n\n\n";
}

No comments:

Post a Comment