In this article, we will show you, How to write a Palindrome Program in Python using While Loop, Functions and Recursion.
Python Palindrome Number
Any number can be Palindrome in python, if it is remained same when we reversed it. For example 131 is a palindrome number because, this number remains same after reversing it.
The following steps will show you the common approach to check for the Python Palindrome Number
- Enter any number
- Reverse a given number
- Compare original value with reverse value.
- If they exactly matched then it is Palindrome number otherwise, it is not Palindrome number.
Python Palindrome Program using While Loop
This Palindrome program in Python allows the user to enter any positive integer.
Next, this palindrome in python program will check whether a given number is Palindrome Number or Not using While Loop
# Python Palindrome Program using While loop number = int(input("Please Enter any Number: ")) reverse = 0 temp = number while(temp > 0): Reminder = temp % 10 reverse = (reverse * 10) + Reminder temp = temp //10 print("Reverse of a Given number is = %d" %reverse) if(number == reverse): print("%d is a Palindrome Number" %number) else: print("%d is not a Palindrome Number" %number)
User Entered value in this palindrome program in Python: Number = 191 and Reverse = 0
Temp = Number
Temp = 191
Reminder = Temp %10
Reminder = 191 % 10 = 1
Reverse = Reverse *10 + Reminder
Reverse = 0 * 10 + 1
Reverse = 0 + 1
Reverse = 1
Temp = Temp //10
Temp = 191 /10
Temp = 19
From the first Iteration the values of both Temp and Reverse has been changed as:
Temp = 19 and Reverse = 1
Reminder = 19 % 10 = 9
Reverse = 1 * 10 + 9 = 10 + 9
Reverse = 19
Temp = 19 /10
Temp = 1
From the Third Iteration of palindrome program in Python, the values of both Temp and Reverse has been changed as:
Temp = 1 and Reverse = 19
Reminder = 1 % 10 = 1
Reverse = 19 * 10 + 1 = 190 + 1
Reverse = 191
Temp = 1/10
Temp = 0
Here Number = 0 so, the while loop condition will fail
if ( Number == Reverse ) – condition will check whether the user enter number is exactly equal to Reverse number or not. If this condition is True, then it is Palindrome else the given number is not Palindrome number in Python
Palindrome Program in Python using Functions
# 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("Please Enter any Number: ")) rev = integer_reverse(number) print("Reverse of a Given number is = %d" %rev) if(number == rev): print("%d is a Palindrome Number" %number) else: print("%d is not a Palindrome Number" %number)
Palindrome Program in Python using Recursion
In this Python Palindrome program we are checking whether a given number is Palindrome Number or Not using Recursive Functions concept.
# Python Program to print Palindrome numbers from 1 to 100 maximum = int(input(" Please Enter the Maximum Value : ")) print("Palindrome Numbers between 1 and %d are : " %maximum) for num in range(1, maximum + 1): temp = num reverse = 0 while(temp > 0): Reminder = temp % 10 reverse = (reverse * 10) + Reminder temp = temp //10 if(num == reverse): print("%d " %num, end = ' ')
In this Python palindrome program, within the function declaration,
integer_reverse (Number // 10) – it will help to call the function Recursively with updated value. If you miss this statement then, after completing the first line it will terminate. For example, Number = 191 then the output will be 1
Lets see the If condition
if (Number > 0) will check whether the number is greater than 0 or not. For Recursive functions it is very important to place a condition before using the function recursively otherwise, we will end up in infinite execution (Same like infinite Loop).
Please be careful :)