What's wrong with my code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

23rd Oct 2020, 3:14 AM
Manoj Kumar
Manoj Kumar - avatar
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.
23rd Oct 2020, 3:24 AM
777
777 - avatar
+ 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);
23rd Oct 2020, 4:53 AM
ChaoticDawg
ChaoticDawg - avatar