// Iteration on Fiboncci.cpp : Defines t
// he entry point for the console applicati
// on.
//
#include "stdafx.h"
#include "iostream.h"
long double fib(int position);//this has to be global
int main(int argc, char* argv[])
{
int position, choice;
long double answer;
loop: cout << "Which Position? ";
cin >> position;
cout << "\n";
answer = fib(position);//call to function
cout << answer << " is the ";
cout << position << "th Fibonicci number.\n\n";
//asks for doing it again
cout << "Again? \n(1) yes \n(2) exit\n";
cin >> choice;
if (choice == 1)
goto loop;
else
cout << "\nExiting...";
return 0;
}
long double fib(int n)
{
long double minusTwo=1, minusOne=1, answer=2;
if (n<3)
return 1;
for (n -= 3;n; n--)// goes through counting up the series until reaching the number.
{
minusTwo=minusOne;
minusOne=answer;
answer = minusOne + minusTwo;
}
return answer;
}