Palindrome number | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Palindrome number

Write a program to determine if given number is palindrome or not. Print true if it is palindrome, false otherwise. Palindrome are the numbers for which reverse is exactly same as the original one. For eg. 121

2nd Jul 2021, 10:58 AM
Student
Student - avatar
5 Answers
+ 3
def is_palindrome(n): return n == n[::-1] n = input() print(is_palindrome(n))
2nd Jul 2021, 3:30 PM
David Ashton
David Ashton - avatar
+ 2
Similar to the other solution I gave you earlier for your other question, you can use the modulo operator to get the digit in the one's place (n = num % 10) then set this digit to a variable. You can then drop the one's digit by using floor division (num //= 10). Multiply the saved digit by 10 to move it over to prepare the addition of the next number in the one's place. Repeat until num is 0 and the variable hold the reversed value of num. Then return the compared original input value of num (only alter a copy of it to preserve it) to the reversed value of num. If they are equal then the number is a palindrome.
2nd Jul 2021, 12:36 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Shadoff pretty sure they are needing it done mathematically. Otherwise, I would have offered a 1 liner 😉
2nd Jul 2021, 1:18 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Here is the hint def checkPalindrome(num): #Implement Your Code Here num = int(input()) isPalindrome = checkPalindrome(num) if(isPalindrome): print('true') else: print('false')
2nd Jul 2021, 10:59 AM
Student
Student - avatar
2nd Jul 2021, 1:09 PM
Shadoff
Shadoff - avatar