I am having a bit of trouble solving Project Euler question 4. I am inexperienced in programming and didn\'t really understand other answers. I managed to write a code that
Here is a code to check PALINDROME
-ness of a number without converting it to a String
.
bool checkPalindrome(int n)
{
int num = n;
int s = 0;
while(num!=0)
{
s = s*10 + (num%10);
num = num/10;
}
if(s==n)
return true;
return false;
}
As you can see that if n
= 120
, the reverse is calculated as s
= 21
instead of 021
, but that is fine, keeping in mind that a palindrome number won't end in 0
, because if it does, then it would have to begin with 0
which makes it an invalid number!!!!
Hope this helps!!!