0
What's wrong with my code?
It's returning true with positive numbers and false with negative numbers! But true was expected in both the cases! It's a program to test if given is palindrome. https://code.sololearn.com/c8qQY2tqkOZp
2 Answers
+ 3
Logically speaking, negative numbers aren't palindromes. However, if you see them by ignoring "-" sign, then your code should also ignore the sign, for that you have to take absolue value!
You can add,
number = Math.abs(number);
at the start of function.
+ 2
When the number is negative your while loop isn't entered due to;
while(n != 0 && n > 0) n > 0 is false
So, as @vrintle stated use the absolute value of the number passed in.
int n = Math.abs(number);