I am working on an assignment (I know many have asked but none have asked about the number of digits I need plus some) that is to accept input for the fibonacci corresponding to that (i.e. 0=0, 1=1, 2=1, ..., 96=51680708854858323072). When it gets to 79 (should be 14472334024676221 since 77=5527939700884757 and 78=8944394323791464) I end up getting 14472334024676220. I believe this is because of precision. I know this is not as little code as could be used to solve this but it is how the assignment was set up.
long double whatFib ( long double checkFib );
int main()
{
long double userFib;
do
{
cout << "Which Fibonacci number would you like? ";
cin >> userFib;
}
while ( userFib < 0 || userFib > 96 );
cout.precision(0);
cout << "Fibonacci #" << userFib << " is " << fixed << whatFib(userFib);
cin >> userFib;
return 0;
}
long double whatFib( long double checkFib )
{
long double result, fibOne, fibTwo, fibNum;
fibOne = 0;
fibTwo = 1;
if ( checkFib == 0 )
{
result = 0;
}
else if ( checkFib == 1 )
{
result = 1;
}
else
{
for ( fibNum = 1; fibNum <= checkFib; fibNum++ )
{
result = fibOne + fibTwo;
fibTwo = fibOne;
fibOne = result;
cout << "#" << fibNum << ": " << fixed << result << endl;
}
}
return result;
}