A palindromic number is a symmetrical number which remains same when it’s digits are reversed.
Example : 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 11 , 22 , 33 , 44 , 55 , 66 , 77 , 88 , 99 , 101 , 111 , 121 , 131 , 141 , 151 , 161 , 171 , 181 , 191 , 202……..
Today I will show you how to check if a number is a palindromic number, how to get the next palindromic number of a number, how to get the previous palindromic number of a number.
Function is_palindromic :
[python]
def is_palindromic(number):
string = str(number)
if number == int(string[::-1]):
return True
else:
return False
[/python]
This function will return true for palindromic number otherwise it will return false.
Example :
[python]
## Example One…
if is_palindromic(20):
print "The number is a palindromic number."
else:
print "The number is not a palindromic number."
//// It will print "The number is not a palindromic number."
## Example Two…
if is_palindromic(22):
print "The number is a palindromic number."
else:
print "The number is not a palindromic number."
//// It will print "The number is a palindromic number."
[/python]
Function next_palindromic() :
[python]
def next_palindromic(number):
i = 0
while i < 1:
number = number + 1
string = str(number)
if number == int(string[::-1]):
i = 1
return number
[/python]
This function will produce the next palindromic number of a number.
Example :
[python]
print next_palindromic(11)
//// It will print 22
print next_palindromic(22)
//// It will print 33
[/python]
Function prev_palindromic() :
[python]
def prev_palindromic(number):
i = 0
while i < 1:
number = number – 1
string = str(number)
if number == int(string[::-1]):
i = 1
return number
[/python]
This function will produce the previous palindromic number from a number.
Example :
[python]
print prev_palindromic(22)
//// It will print 11
print prev_palindromic(2)
//// It will print 1
[/python]
