In this article, we will discuss what palindrome numbers are and how to write a Palindrome program in Python i.e. how to write a program to detect whether a number is a palindrome or not in Python.

## 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).

So, this is how we can write a palindrome program in Python. We hope that you have understood the concept completely and have written and understood the code as well. Hope to see you again at PrepBytes.