Palindrome Program in Python using while loop,  Function, etc

Palindrome Program in Python using while loop, Function, etc

Before we write palindrome programs in python, we should know what is a palindrome?

What is a palindrome?

Answer:- Any number could be Palindrome in python if it remained the same when reversed it. i.e., 252 is a palindrome number because this number remains the same after reversing it.

Now, you will see Python programs that cover the following:

  • Python Palindrome Program using While Loop
  • Palindrome Program in Python using function

1: Python Palindrome Program using While Loop

  1. Take an input number from user.
  2. Reverse a given number using while loop
  3. Compare the original number with reverse number.
  4. If both numbers exactly matched, then it is a Python Palindrome number. Otherwise, it is not a Palindrome number.
num=int(input("Enter any number:"))
temp=num
rev=0
while(num>0):
    dig=num%10
    rev=rev*10+dig
    num=num//10
if(temp==rev):
    print("The {0} number is palindrome!".format(temp))
else:
    print("Not a palindrome!")

Output

Enter any number: 121
This 121 number is palindrome!

2: Palindrome Program in Python using Function

  1. Take an input number from user.
  2. Reverse a given number using function
  3. Compare the original number with reverse number.
  4. If both numbers exactly matched, then it is a Python Palindrome number. Otherwise, it is not a Palindrome number.
# Python Palindrome Program using Functions
 
reverse = 0
def integer_reverse(number):
    global reverse
    
    if(number > 0):
        Reminder = number % 10
        reverse = (reverse * 10) + Reminder
        integer_reverse(number // 10)
    return reverse


number = int(input("Enter any Number: "))

rev = integer_reverse(number)

if(number == rev):
    print("The %d is a Palindrome Number" %number)
else:
    print("The %d is not a Palindrome Number" %number)

Output

Please Enter any Number:  151
The 151 is a Palindrome Number

Leave a Reply

Your email address will not be published. Required fields are marked *