# next palindrome number

• 09-04-2006
kris.c
next palindrome number
I am given a lower limit and an upper limit.. (integers)
I am given a palindrome in this interval..
I am supposed to find the next palindrome in this interval
...
Cud think of something for smaller intervals when I know the number of digits in the number is 3,4,5...
Can some one give a generic method if the upper limit is given to be of the order 10^5..
• 09-04-2006
Salem
Was there a question in there?
• 09-04-2006
dwks
Quote:

the upper limit is given to be of the order 10^5
In that case you'll probably want to use longs.

Finding the next palindrome is pretty easy. Say you have the number 123321; in that case, the next palindrome would be 124421. For 12321, it would be 12421. If you count the digits in the original palindrome you can figure out the middle digit[s] and increment them.

It's a little harder when the number's something like 15951. You have to check if the middle number[s] are 9; if they are, increment the next columns: 16061.

You also have to handle a number like 999, which would become 1001. You'd probably want a special case for this, when all the digits are 9.

Hope this helps.
• 09-04-2006
sisovich
A brute force approach would be
Starting N
Convert N to a string using sprintf (Nstr)
Check if Nstr is a "palindrome string"
Increase N and go back to the first until your final condition

---------------------------------
http://www.uberum.com
• 09-04-2006
dwks
Well, that would be pretty silly. But yes, it's one way to do it.
• 09-05-2006
jafet
[offtopic] Salem -- cool avatar ;) [/offtopic]

You might want to use strings for this -- handling specific digits in an integer may become tricky. You could use strcmp() to check the range as strings can't use the normal operators.