Hello there,
I’m going over EPI in Java. (This is my first question, so wanted to say big thanks to putting this wonderful book together to the authors )
For problem 4.8, reverse digits, is there a particular reason why we want to use the absolute value of the input and apply the sign at the end?
If we have a negative number as an input, modulo of negative number will also be negative, and negative number * 10 is also a negative, so it all seems to work out nicely without having to drop the sign and apply it back at the end. The code below passed the epi judge. Is there something that I am missing?
long reversedX = 0;
while ( x != 0 ) {
reversedX = reversedX * 10 + ( x % 10 );
x /= 10;
}
return reversedX;