0

I'm trying to check if a number is a palindrome. I have written the code to check if a string is a palindrome using recursion, but I'm having a hard time writing the one to check for numbers.

I'm passing just the number to the method, nothing else (eg, number of digits).

Anyone got any suggestions for me?

user2027425
  • 389
  • 3
  • 5
  • 14

2 Answers2

0

You can convert your number to a string and pass the string to your already existing function you have written to test if a string is a palindrome.

You convert a number to a string using STR(NUMBER_HERE)

Zeddy
  • 2,079
  • 1
  • 15
  • 23
0

Why do you have to do it with recursion?

Here's what I would do (in pseudocode):

  1. Set var to middle digit place if size is odd, otherwise, half of size if even (e.g. 3 if the number has 5 digits, 2 if it's 4).
  2. Loop: if odd, we skip the middle digit for (i = 0; i < digitsArray.size /2; i++){ if (digitsArray[n-i]!=digitsArray[n+i]) break;
  3. Then if we popped out before the end, it failed.

Something like that.

Rob
  • 11,446
  • 7
  • 39
  • 57
  • Because I know I can do this without recursion. I'm new to concept of recursion and I'm trying to learn as much about it as possible. – user2027425 Jan 31 '13 at 00:25
  • Yeah you're right. I was thinking of words, like madam, but I guess noon is a palindrome too. – Rob Jan 31 '13 at 00:29