Fibonacci Sequence Question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Fibonacci Sequence Question

I made a program to calculate the value of a position in the Fibonacci Sequence but when i enter numbers like 200, some values tend to be negative. Why is that? Here's the link for the code: https://code.sololearn.com/c3YV2YRVxhvP/#cpp

24th Mar 2020, 7:17 PM
Oscar Ramírez Díaz
Oscar Ramírez Díaz - avatar
11 Answers
+ 3
That is a typical case of integer overflow, where the result of the addition is bigger than what can be stored inside a 32-bit integer variable, so the number is rolled back. Also see the following: https://stackoverflow.com/questions/29235436/c-integer-overflow
24th Mar 2020, 7:31 PM
Shadow
Shadow - avatar
+ 5
the value seems to be too big for an Integer variable. try with long or unsigned long.
24th Mar 2020, 7:31 PM
John Robotane
John Robotane - avatar
+ 3
Absolutely agree Shadow... I don't seem to have the same issue in my Perl code
24th Mar 2020, 7:33 PM
BroFar
BroFar - avatar
+ 2
Looks like an overflow issue.
24th Mar 2020, 7:31 PM
BroFar
BroFar - avatar
+ 2
Oscar Ramírez Díaz stick around and learn from your peers and professionals. Some of us have been programming for decades while others for a year. By the time you get in out of University you should have numerous skillsets by the time you enter the workforce.
24th Mar 2020, 8:30 PM
BroFar
BroFar - avatar
+ 1
Well, I don't know any Perl at all, but I guess it has some smarter integer handling then, BroFar. :)
24th Mar 2020, 7:46 PM
Shadow
Shadow - avatar
+ 1
Here is my code even after stretching it out several times over attempting to reach an overflow.... Shadow https://code.sololearn.com/cSB0ZKlT7fMR/?ref=app
24th Mar 2020, 8:19 PM
BroFar
BroFar - avatar
+ 1
I'm learning for myself, I'm about to enter university, at which grade should I be able to do things like that?
24th Mar 2020, 8:23 PM
Oscar Ramírez Díaz
Oscar Ramírez Díaz - avatar
+ 1
I mean like the code you did
24th Mar 2020, 8:23 PM
Oscar Ramírez Díaz
Oscar Ramírez Díaz - avatar
0
Holly cow
24th Mar 2020, 8:21 PM
Oscar Ramírez Díaz
Oscar Ramírez Díaz - avatar
24th Mar 2020, 8:22 PM
BroFar
BroFar - avatar