Chapter #6 – Flow of Control

Class 11 – Chapter 6 Solutions – PROGRAMMING EXERCISES #8
Write a function that checks whether an input number is a palindrome or not.
Python Program:
def is_palindrome(number):
# Convert the number to a string to check for palindrome
str_number = str(number)
# Check if the string is equal to its reverse
return str_number == str_number[::-1]
# Get user input
try:
user_input = int(input("Enter an integer number: "))
if is_palindrome(user_input):
print(f"{user_input} is a palindrome.")
else:
print(f"{user_input} is not a palindrome.")
except ValueError:
print("Invalid input. Please enter an integer.")
You can checkout the code from Github here – Programming Exercises – 8
Explanation of the Code:
1. Function Definition: The is_palindrome function takes an integer number as
input and converts it to a string.
2. Palindrome Check: It checks if the string representation of the number is
equal to its reverse (str_number[::-1]).
3. User Input: The program prompts the user to enter an integer and checks if
the input is valid.
4. Output: Based on the result of the palindrome check, it prints whether the
input number is a palindrome or not.
Example Output:
Enter an integer number: 121
121 is a palindrome.
Enter an integer number: 123
123 is not a palindrome.
NCERT Computer Science Class 11 Chapter 6 Solutions PE Q 8