Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence.
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)));
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #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))); } } |
1 Response
[…] Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence. […]