Problem Statement:
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...
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...
Solution:
//Ahmad Furqan
//P5.c
#include <iostream>
#include <conio.h>
using namespace std;
int fabb[25],index=0;
void fabb_rec(int a, int b)
{
if (index == 25)
return;
fabb[index] = a + b; //add previous terms
//update last two terms
a = b;
b = fabb[index];
index++;
return fabb_rec(a, b);
}
void main(void)
{
cout << "First 25 numbers of a Fibnocci sequence are: " << endl;
index = 2;
fabb[0] = 1;
fabb[1] = 1;
fabb_rec(1, 1);
for (int i = 0; i < 25; i++)
cout << " " << fabb[i];
_getch();
}
No comments:
Post a Comment