How are Fibonacci numbers becoming negative after some time ? Can anyone explain it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How are Fibonacci numbers becoming negative after some time ? Can anyone explain it?

Recently I saw Fibonacci numbers may be negative but I can't understand how they become negative . So explain it deeply that's why I can understand Thank you https://code.sololearn.com/cyD6Ol5V38el/?ref=app

13th Nov 2021, 3:24 AM
Sâñtôsh
Sâñtôsh - avatar
5 Answers
+ 5
I'm thinking you are witnessing integer overflow phenomenon https://blog.webnersolutions.com/java-int-and-float-overflow-and-underflow-with-examples/
13th Nov 2021, 3:36 AM
Ipang
+ 3
Jay Matthews Ipang Thank you so much
13th Nov 2021, 4:16 AM
Sâñtôsh
Sâñtôsh - avatar
+ 3
use long instead int and by Math.addExact(a,b) it produces Exception for overflow then can catch the Exception by try - catch statements sum = Math.addExact(sum, b); long temp = b; b = Math.addExact(a,b); a = temp; also for higher numbers there are class java.math.BigInteger
13th Nov 2021, 8:18 AM
zemiak
+ 1
Jay Matthews Overflow in java?? I thought that java doesn't need dynamically allocated memory. Why java's int is not like other languages like JavaScript and Python ?? Java is not compiled. So it doesn't have to use int data type like compiles languages Correct me if I'm wring
13th Nov 2021, 4:49 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
0
hi
18th Nov 2021, 11:25 AM
Sk Sathar
Sk Sathar - avatar