Why my code is not running correctly for all inputs? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Why my code is not running correctly for all inputs?

https://code.sololearn.com/ch35eJnswQ50/?ref=app For inputs 2,3,4, the output is exactly as expected but for different inputs like 5,10,3 ,the output is not as expected. And also after giving inputs 5,10,10, there are some negative values in the output.

5th Apr 2020, 7:25 AM
Roshini Balivada
4 Answers
+ 2
And for inputs 5,10,10, the output values are negative, please help me to solve.
5th Apr 2020, 8:40 AM
Roshini Balivada
+ 1
Your for loop should be like this: for(int i=0; i<c+2; i++) Because i represents how many numbers you want to show. It should not depend on the first value (a)
5th Apr 2020, 8:03 AM
Tibor Santa
Tibor Santa - avatar
+ 1
The issue is in your for loop. You set i to start at a (the first value), so the higher the first value the fewer term will be printed.
5th Apr 2020, 8:06 AM
Jared Bird
Jared Bird - avatar
+ 1
The reason you are getting negative values on 5, 10, 10 is because the calculation overflows the 32 bit int. You can use long for 64 bit integers, this will give you more correct values, but will overflow as well. To handle really big integers Java has a BigInteger class.
5th Apr 2020, 8:55 AM
Jared Bird
Jared Bird - avatar