Last Updated on May 17, 2023 by Prepbytes
A palindrome number is a number that reads the same forwards and backward. In other words, it remains unchanged when its digits are reversed. For example, 121, 454, and 12321 are all palindrome numbers.
Palindrome numbers have interesting properties and are commonly used in number-related puzzles and programming exercises. They can also be found in various real-world applications, such as identifying symmetrical patterns or in certain number systems.
What are Palindrome Numbers?
Palindrome numbers are those numbers that read the same from front and back. For instance, in the above example, the number 121 is a palindrome as when we read it from left to right, it reads 121 and from right to left also, it reads 121 only.
However, the number 1212 is not a palindrome. This is because from left to right, it reads 1212 however from right to left, it reads 2121.
So, now that we know what palindrome numbers are, let us now understand the approach to writing the palindrome program in Python.
Understand with Example
You are given a number as input. You have to tell whether the number is Palindrome or not. For instance, consider the 2 inputs shown below.
So, before understanding the solution, let us first understand what are the palindrome numbers.
How to Check Given Number is Palindrome using Reversal Method
As we can see that the meaning of a palindrome is so simple, and so is the approach to this solution. We can clearly see from the above example that a palindrome will remain the same if we reverse the digits.
For instance, if there is a number 1002 and we reverse its digits, the number becomes 2001. Since the number and its reverse are not equal, this number is not a palindrome. However, the number 1001 on reversing gives 1001. Since the number and its reverse are equal, 1001 is a palindrome.
So, now we just need to understand the algorithm to reverse a number. This is shown below.
Reverse a Number in Python
In order to reverse a number in Python, we will follow the following algorithm.
- Initialize the variable reverse = 0.
- Take the modulus of the number by 10. This will be stored in the variable rem i.e. remainder.
- Now, do rev = rev * 10 + rem.
- Divide the number by 10. Please note that here you have to perform integer or floor division i.e. don’t do N/10, instead do N//10.
- Repeat the steps from 2 to 4 till the number becomes 0.
So, let us take example 98634 as shown below.
So, in the first step, we have rev = 4, and the number is reduced to 9863. Now, let us divide the number by 10 again.
So, in the second step, we have rev = 43, and the number is reduced to 986. Now, let us divide the number by 10 again.
So, in the third step, we have rev = 436, and the number is reduced to 98. Now, let us divide the number by 10 again.
So, in the fourth step, we have rev = 4368, and the number is reduced to 9. Now, let us divide the number by 10 again.
So, in the fourth step, we have rev = 43689, and the number is reduced to 0. Since the number has now become 0, we will stop the division.
So, now that we know how to reverse a number in Python, what we have to do is to reverse the input number. If the reverse is the same as the input number, then we can say that the input number is a palindrome, else it is not a palindrome.
Now that we have understood the procedure, let us write the code for the same.
Program to Check Palindrome Number in Python
N = int(input()) rev = 0 oN = N rem = 0 while N > 0: rem = N % 10 rev = rev * 10 + rem N //= 10 if rev == oN: print("The number is a palindrome") else: print("The number is not a palindrome")
Time Complexity: The time complexity is O(log10N). This is because to reverse a number, we have to extract all the digits of the number by dividing it by 10 till it becomes 0.
Space Complexity (Auxiliary Space): Since we have note used any extra space, the auxiliary space or the extra space is O(1).
In conclusion, checking whether a number is a palindrome involves comparing the number to its reverse. If the number remains the same when its digits are reversed, it is considered a palindrome number.
To check for palindromes, you can compare the digits from the beginning and the end of the number. If the corresponding digits match throughout, the number is a palindrome. Leading zeros are typically ignored when checking for palindromes.
Palindrome numbers are interesting and commonly used in puzzles, algorithms, and programming challenges. They have various applications, including identifying symmetrical patterns and number-related computations.
Frequently Asked Questions
Q1. Can negative numbers be palindromes?
Ans. No, negative numbers cannot be palindromes because the ‘-‘ sign will not be present when the digits are reversed. Palindromes are typically defined as non-negative integers.
Q2. Do leading zeros affect the determination of a palindrome number?
Ans. Leading zeros are generally ignored when determining a palindrome number. For example, "010" is considered a palindrome because the digits "0" and "1" are the same when read forwards and backward.
Q3. How can I check if a number is a palindrome using programming?
Ans. In most programming languages, you can convert the number to a string and compare it with its reverse using string manipulation techniques. Alternatively, you can use mathematical operations to reverse the digits of the number and compare it with the original number.
Q4. Can palindrome numbers be of any length?
Ans. Yes, palindrome numbers can be of any length. They can range from single-digit numbers to very large numbers with multiple digits.
Q5. Are there any mathematical properties or formulas related to palindrome numbers?
Ans. Palindrome numbers do not have specific mathematical properties or formulas. However, they are often used in mathematical puzzles, algorithms, and problem-solving scenarios.
Other Python Programs
Python program to reverse a number
Python program for heap sort
Python program to check armstrong number
Python program to check leap year
Python program to convert celsius to fahrenheit
Python program to find factorial of a number
Python program to reverse a linked list
Python Program to find the middle of a linked list using only one traversal
Python Program to Add Two Numbers
Python Program to Print the Fibonacci Series