What is palindrome integer in python?

This is a Python Program to check whether a given number is a palindrome.

Problem Description

The program takes a number and checks whether it is a palindrome or not.

Problem Solution

1. Take the value of the integer and store in a variable.
2. Transfer the value of the integer into another temporary variable.
3. Using a while loop, get each digit of the number and store the reversed number in another variable.
4. Check if the reverse of the number is equal to the one in the temporary variable.
5. Print the final result.
6. Exit.

Program/Source Code

Here is source code of the Python Program to check whether a given number is a palindrome. The program output is also shown below.

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

Program Explanation

1. User must first enter the value of the integer and store it in a variable.
2. The value of the integer is then stored in another temporary variable.
3. The while loop is used and the last digit of the number is obtained by using the modulus operator.
4. The last digit is then stored at the one’s place, second last at the ten’s place and so on.
5. The last digit is then removed by truly dividing the number with 10.
6. This loop terminates when the value of the number is 0.
7. The reverse of the number is then compared with the integer value stored in the temporary variable.
8. If both are equal, the number is a palindrome.
9. If both aren’t equal, the number isn’t a palindrome.
10. The final result is then printed.

Runtime Test Cases

 
Case 1
Enter number:121
The number is a palindrome!
 
Case 2
Enter number:567
The number isn't a palindrome!

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Next Steps:

  • Get Free Certificate of Merit in Python Programming
  • Participate in Python Programming Certification Contest
  • Become a Top Ranker in Python Programming
  • Take Python Programming Tests
  • Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

What is palindrome integer in python?

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Sometimes, we have an application of checking a number is palindrome or not and it’s quite common while day-day programming or competitive programming, it’s easy to reverse a number and check it but sometimes for readability and reducing lines of code, we require to perform this in one-liner logic. Let’s discuss certain ways in which this can be achieved.

    Input : test_number = 12321
    Output : True

    Input : test_number = 1234
    Output : False

    Method #1 : Using math.log() + recursion + list comprehension
    The combination of above three functions can perform this particular task easily, the logs function extracts the number of digits which is powered by 10 to get the number for that iteration for comparison. The process is recurred to test for palindrome.

    import math

    def rev(num):

        return int(num != 0) and ((num % 10) * \

                 (10**int(math.log(num, 10))) + \

                              rev(num // 10))

    test_number = 9669669

    print ("The original number is : " + str(test_number))

    res = test_number == rev(test_number)

    print ("Is the number palindrome ? : " + str(res))

    Output:

    The original number is : 9669669
    Is the number palindrome ? : True
    

     
    Method #2 : Using str() + string slicing
    This can also be done by converting the number into a string and then reversing it using the string slicing method and comparing it, truth of which returns the answer to it.

    test_number = 9669669

    print ("The original number is : " + str(test_number))

    res = str(test_number) == str(test_number)[::-1]

    print ("Is the number palindrome ? : " + str(res))

    Output:

    The original number is : 9669669
    Is the number palindrome ? : True
    

    We can also read input as string and then simply check for palindrome.

    num = input("Enter a number")

    if num == num[::-1]:

        print("Yes its a palindrome")

    else:

        print("No, its not a palindrome")


    What is a palindromic integer?

    A palindrome integer is an integer x for which reverse(x) = x where reverse(x) is x with its digit reversed. Negative numbers are not palindromic. Example : Input : 12121 Output : 1. Input : 123 Output : 0.

    What is palindrome number example?

    A palindrome number is a number that remains the same when digits are reversed. For example, the number 12321 is a palindrome number, but 1451 is not a palindrome number.

    Is there palindrome function in Python?

    3. Using Built-in Functions. We can also check palindrome in Python with the help of the built-in reversed() function provided for sequence data types in Python: reversed(sequence) : It returns an iterator that accesses the sequence passed as an argument in the reverse order.

    How do you print a palindrome in Python?

    Palindrome Program.
    str = 'JaVaJ'.
    strstr = str.casefold().
    # This string is reverse..
    rev = reversed(str).
    if list(str) == list(rev):.
    print("PALINDROME !").
    print("NOT PALINDROME !").