Here’s an example Python code to check if a given number is a palindrome:
# Define a function to check if a number is a palindrome def is_palindrome(num): # Convert the number to a string and reverse it reversed_num_str = str(num)[::-1] # Compare the original and reversed strings return str(num) == reversed_num_str # Test the function with some sample numbers num1 = 12321 num2 = 12345 if is_palindrome(num1): print(num1, "is a palindrome.") else: print(num1, "is not a palindrome.") if is_palindrome(num2): print(num2, "is a palindrome.") else: print(num2, "is not a palindrome.")
In this code, we define a function is_palindrome()
that takes one argument num
and returns True
if the number is a palindrome, and False
otherwise. To check if the number is a palindrome, we first convert it to a string using the str()
function. We then use slicing with a step value of -1 to reverse the string. Finally, we compare the original string and the reversed string to check if they are equal.
In the main program, we test the is_palindrome()
function with some sample numbers num1
and num2
. We use an if
statement to check if each number is a palindrome, and use the print()
function to output the result.
When you run this code, it should output:
12321 is a palindrome. 12345 is not a palindrome.
Note that you can replace the sample numbers with any other numbers you want to test the function with.