
Patrick B. answered 05/03/20
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#define MAX (47) //MAX that can be stored before overflow
//returns the Nth fibonacci number
long Fib(int n)
{
int iReturn=-1;
if ((n==1) || (n==2))
{
iReturn=1;
}
else if (n>1)
{
iReturn = Fib(n-1)+ Fib(n-2);
}
return(iReturn);
}
int main()
{
long A[MAX]; //stores the fibonacci numbers
// is 1-based, so the array index corresponds
// directly with the position of the fibonacci #
//1,1,2,3,5,6
cout << Fib(6) << endl; //Fib(6) = 8
//.... and here is your array
A[1]=A[2] = 1;
for (int iLoop=3; iLoop<MAX; iLoop++)
{
A[iLoop] = A[iLoop-1]+A[iLoop-2];
cout << A[iLoop] << endl;
}
}