Palindrome Number Program in Python (2024)

Last Updated on May 17, 2023 by Prepbytes

Palindrome Number Program in Python (1)

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.

Palindrome Number Program in Python (2)

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.

  1. Initialize the variable reverse = 0.
  2. Take the modulus of the number by 10. This will be stored in the variable rem i.e. remainder.
  3. Now, do rev = rev * 10 + rem.
  4. 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.
  5. Repeat the steps from 2 to 4 till the number becomes 0.

So, let us take example 98634 as shown below.

Palindrome Number Program in Python (3)

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.

Palindrome Number Program in Python (4)

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.

Palindrome Number Program in Python (5)

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.

Palindrome Number Program in Python (6)

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.

Palindrome Number Program in Python (7)

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

  • Python
N = int(input())rev = 0oN = Nrem = 0while 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).

Conclusion
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

Palindrome Number Program in Python (2024)

FAQs

How do you write a palindrome number program in Python? ›

Palindrome Program using While Loop

num=int(input("Enter a number:")) temp=num rev=0 while(num>0): dig=num%10 rev=rev*10+dig num=num//10 if(temp==rev): print("The number is palindrome!") else: print("Not a palindrome!")

How do you print a palindrome pattern in Python? ›

Program to print palindrome pyramid pattern using numbers
  1. scanf(“%d”, &n); printf(” “);
  2. for (i=1; i<=n; i++) {
  3. for (j=1; j<=n-i; j++) printf(” “);
  4. for (j=1,k=2*i-1; j<=2*i-1; j++,k–) {
  5. printf(“%d”, j); else.
  6. printf(“%d”, k); }
Jul 19, 2023

How do you check if a list of numbers is a palindrome in Python? ›

Method 3 : Using reverse() method

One of the best approach is to reverse the list and then compare it to the original list. If they match, the list is a palindrome.

What is palindrome in Python example? ›

For a number or string to be palindrome in Python, the number or string must be the same when inverted. If the string or number does not remain unaltered when reversed, it is not a Palindrome. For instance, 11, 414, 1221, and 123321 are palindrome numbers in Python.

How do you write a palindrome code? ›

Palindrome number algorithm
  1. Get the number from user.
  2. Hold the number in temporary variable.
  3. Reverse the number.
  4. Compare the temporary number with reversed number.
  5. If both numbers are same, print palindrome number.
  6. Else print not palindrome number.

How to create a palindrome in Python? ›

How do you write a palindrome number in Python?
  1. Read the letter or number.
  2. Hold it in a temporary variable.
  3. Reverse it and compare its temporary variable with the reversed version.
  4. If both of them match, it is a palindrome.
Mar 19, 2024

How to find the palindrome of a number? ›

A palindrome number is a number that reads the same forward and backward. In other words, if you reverse the digits of a palindrome number, you get the same number. For example, 121 is a palindrome number because if you read it backward, it is still 121. Another example of a palindrome number is 132231.

How do you print a palindrome between two numbers? ›

Approach:
  1. Use a for loop and pass every element to the palindrome function.
  2. Now make a palindrome function which takes an integer as a parameter.
  3. Reverse the given number.
  4. If the reversed number is equal to the given number then print it.

What are examples of palindrome? ›

Examples are civic, radar, level, rotor, kayak, madam, and refer. The longest common ones are rotator, deified, racecar, and reviver; longer examples such as redivider, kinnikinnik, and tattarrattat are orders of magnitude rarer.

How to call a function in Python? ›

To call a function in Python, you simply type the name of the function followed by parentheses (). If the function takes any arguments, they are included within the parentheses.

What is an example of a palindrome string? ›

A string is called a palindrome if the reverse of the string is the same as the original one. Example: “madam”, “racecar”, “12321”.

How do you create a palindrome in Python? ›

How do you write a palindrome number in Python?
  1. Read the letter or number.
  2. Hold it in a temporary variable.
  3. Reverse it and compare its temporary variable with the reversed version.
  4. If both of them match, it is a palindrome.
Mar 19, 2024

What is palindrome number in programming? ›

Palindrome number in java: A palindrome number is a number that is same after reverse. For example 545, 151, 34543, 343, 171, 48984 are the palindrome numbers. It can also be a string like LOL, MADAM etc.

What is palindrome in programming? ›

It means that when you reverse a given string, it should be the same as the original string. For instance, the string 'level' is a palindrome because it remains the same when you read it from the beginning to the end and vice versa.

Top Articles
Latest Posts
Article information

Author: Greg O'Connell

Last Updated:

Views: 5866

Rating: 4.1 / 5 (42 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Greg O'Connell

Birthday: 1992-01-10

Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519

Phone: +2614651609714

Job: Education Developer

Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding

Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.