# Extracting the digit without changing the original number

• At the beginning, I extracted digits from the integer by changing the value of input integer x, which would NOT work for the cases where there are zeros within the number, e.g. 100021.

So I figured out that we could extract the digit without changing the original value with the formulas as `digit = (x/div) % 10`, the `div` is the value `10^(digit_position)`.

Based on the above formulas, here is the algorithm.

``````public boolean isPalindrome(int x) {
long rdiv = 1;
long ldiv = 10; // long instead of int, otherwise overflow!  the highest position in x.

// Assume negative number is NOT palindrome
if(x < 0)
return false;
else if(x < 10)
return true;

// Find the position of left division
while( (x/ldiv) > 9 ){
ldiv *= 10;
}

while(ldiv > rdiv){
// extract digits based on the divisions,
//		without changing the value of x.
int l = (int)(x/ldiv)%10;
int r = (int)(x/rdiv)%10;

if(l != r)
return false;
// shift two digits
ldiv /= 10;
rdiv *= 10;
}

return true;
}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.