Recursive function first 25 numbers of a Fibonacci sequence.
Object: Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence. In a Fibonacci sequence the sum of two successive terms gives the third term. Following are the first few terms of the Fibonacci sequence: 1 1 2 3 5 8 13 21 34 55 89…
Code:
#include<iostream>
#include<conio.h>
using namespace std;
int fab(int n);
void main()
{
int num,i=0;
cout<<“Enter Any Number: “;
cin>>num;
while (i<=num)
{
cout<<” “<<fab(i);
i++;
}
getch();
}
int fab(int n)
{
if(n==0||n==1)
{
return n;
}
else
{
return((fab(n-1)+fab(n-2)));
}
}
#include<iostream> #include<conio.h> using namespace std; int fab(int n); void main() { int num,i=0; cout<<"Enter Any Number: "; cin>>num; while (i<=num) { cout<<" "<<fab(i); i++; } getch(); } int fab(int n) { if(n==0||n==1) { return n; } else { return((fab(n-1)+fab(n-2))); } }
Pingback: C++ projects for beginners with source code - Codeboks