Skip to content

Commit 0340eaa

Browse files
authored
Add palindrome number checker with user input
Adds a simple Python implementation to check whether a number is a palindrome using user input.
1 parent 2c15b8c commit 0340eaa

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

maths/palindrome_number.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def is_palindrome(number: int) -> bool:
2+
"""
3+
Check whether a given integer is a palindrome.
4+
5+
A palindrome number reads the same forwards and backwards.
6+
7+
Examples:
8+
>>> is_palindrome(121)
9+
True
10+
>>> is_palindrome(123)
11+
False
12+
"""
13+
number_str = str(number)
14+
return number_str == number_str[::-1]
15+
16+
17+
if __name__ == "__main__":
18+
user_input = input("Enter a number: ").strip()
19+
20+
if user_input.isdigit():
21+
number = int(user_input)
22+
if is_palindrome(number):
23+
print("The number is a palindrome.")
24+
else:
25+
print("The number is not a palindrome.")
26+
else:
27+
print("Please enter a valid non-negative integer.")

0 commit comments

Comments
 (0)