My Ansi C Fibonacci code stinks! obvious bug?
I know I have an obvious bug -- well, a calculation error somewhere. Doing a quick fibonacci code sample, only requirements is NOT to use arrays and NOT to do it recursively.
This is what I came up with, and its obviously wrong!
Code:
int fibonacci(int userinput)
{
int result = 0;
int counter = 0;
if (userinput == 0) return result = 0;
else if (userinput == 1) return result = 1;
else
for (counter = userinput; counter > 1; counter--)
{
result += ((counter - 1) + (counter - 2));
}
return (result);
}
I am sure the "bad guy" is the line result += (...) but what is wrong?