question
Create a method that checks if an input integer is a palindrome without using an
array or converting it to a String.
ex. isPalindrome(1234) returns false
ex 2. isPalindrome(12321) returns true
ex. isPalindrome(1234) returns false
ex 2. isPalindrome(12321) returns true
Try playing with the modulus operator to extract digits.
isPalindrome(input)
reverse = 0;
inTemp = input;
while ( inTemp != 0 )
reverse = (reverse * 10) + (inTemp % 10)
inTemp = inTemp / 10
if ( reverse == input )
print("true")
else
print("false")
This solution works by first extracting the rightmost digit from the input number (ex. 1234 % 10 = 4) and then adding it to the current reversed number multiplied by ten (ex. (0 * 10) + 4 = 4; in second iteration (4 * 10) + (123 % 10) = 43). inTemp which initially stores the input number is divided by ten with each iteration to move on to the next digit. These steps are repeated until inTemp = 0 at which point all digits have been stored in reverse.