I am trying to use fairly simple C++ code to complete this problem: The user will input a number of the Fibonacci series, and through a function called whichfib(), the program will then output the number using long double data type.
I have gotten the code to work until the number 47, some numbers after 47 will be negative, and some are just completely off. The code should only work from fib numbers 0-95, here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
long double whichfib (int num);
int main ()
{
int num, count=-1;
while (count < 0 || count > 96)
{
cout << "Which Fibonacci number would you like? ";
cin >> num;
count = num;
}
cout << setprecision(0);
cout << "Fibonacci #" << num << " is " << fixed << noshowpoint << whichfib (num) << endl;
return 0;
}
long double whichfib (int num)
{
int count=2, newnum1, newnum2, newnum;
long double fnum;
if (num == 0)
fnum = 0;
else if (num == 1)
fnum = 1;
else if (num == 2)
fnum = 1;
else
{
newnum1=0;
newnum2=1;
do
{
newnum = newnum1 + newnum2;
newnum1 = newnum2;
newnum2 = newnum;
count++;
}
while (count < num);
fnum = newnum1 + newnum2;
}
return fnum;
}
if anyone can help it would be greatly appreciated, but please remember this is a beginner course, I cannot put any advanced statements, it must be a while statement and also just what is available in the iostream library. Thanks again