Friday, August 04, 2006

Check if binary representation of an int is a palindrome or not.

if x == reverse of x then x is palindrome

int palindrom(unsigned int n)
{
    unsigned int temp = n;
    unsigned int reverse = 0;
    while(temp) {
        int remainder = temp%10; // get last digit
        temp /= 10;                 // truncate last digit    
        reverse = reverse*10 + remainder; //build reverse value
    }
    return (n == reverse);
}

No comments:

Post a Comment