+ 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.
4 Answers
+ 2
And for inputs 5,10,10, the output values are negative, please help me to solve.
+ 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)
+ 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.
+ 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.