# Number Palindrome

• 05-17-2011
Dontgiveup
Number Palindrome
Hallo, I would like to wrote a programme that has two bool functions, one for decimal and one for binary. The function is supposed to show whether the given parameter is a palindrome in decimal or binary system or not. Now here is the tricky part. No divisions and multiplications allowed.
• 05-17-2011
quzah
Convert it to a string.

Quzah.
• 05-17-2011
Dontgiveup
Convert a number to string. Interesting. I don't really know how I can convert a number to a string. So I have 15, how can I convert it this to a string? I thought they were two separate data types that can't be mixed.
• 05-17-2011
kmdv
In binary (haven't tested, just an idea):

Code:

```inline bool getbit(unsigned x, unsigned bit) {     // gets specified bit starting from 0     return (x & (1 << bit)) != 0; } bool testbin(unsigned x) {     // count number of visible digits     unsigned n = 0;     unsigned tempx = x;     do     {         ++n;         tempx >>= 1;     }     while (tempx > 0)         // using previously computed number, test if palindrome     for (unsigned i = 0; i < n >> 1; i++) // n >> 1 since n / 2 is not allowed     {         if (getbit(x, i) != getbit(x, n - i - 1))             return false;     }     return true; }```
In decimal:

depends whether modulo is also allowed.
• 05-17-2011
Dontgiveup
Thanks but you used division (i < n / 2) and no division is allowed.
• 05-17-2011
cyberfish
You can always implement division and multiplications with shifts. Just horribly inefficient when you have dedicated circuits in your CPU that can do them in hardware.
• 05-17-2011
novacain
Code:

```bool IsPalindrome(int Input) {         char szInput[MAX_PATH] = {'0'};         //convert to string         _snprintf(szInput, MAX_PATH -1, "%d", Input);         //find length         int Len = lstrlen(szInput);         //for each character         for(int i=0;i<Len;i++)         {                 //check to see if corresponding chars match                 if(szInput[i] != szInput[Len-i])                 {                         //is not palindrome                         return false;                 }                 if(i >= (Len - i) )//more than 1/2 way thru string and each char has matched, so is palindrome                 {                         return true;                 }         }         return false; }```
PS I left a bug in the code.......
• 05-18-2011
kmdv
Quote:

Originally Posted by Dontgiveup
Thanks but you used division (i < n / 2) and no division is allowed.

I edited it a few seconds before you posted.
• 05-18-2011
kmdv
Quote:

Originally Posted by novacain
Code:

```bool IsPalindrome(int Input) {         char szInput[MAX_PATH] = {'0'};         //convert to string         _snprintf(szInput, MAX_PATH -1, "%d", Input);         //find length         int Len = lstrlen(szInput);         //for each character         for(int i=0;i<Len;i++)         {                 //check to see if corresponding chars match                 if(szInput[i] != szInput[Len-i])                 {                         //is not palindrome                         return false;                 }                 if(i >= (Len - i) )//more than 1/2 way thru string and each char has matched, so is palindrome                 {                         return true;                 }         }         return false; }```
PS I left a bug in the code.......

It is very likely that printf uses division to do the conversion.