Write a Palindrome Number Program in Python using While Loop, Functions, and Recursion. Any number could be Palindrome if it remained the same when we reversed it. For example, 131 because it remains the same after reversing it.
The common approach to check for the Palindrome Number is to reverse a given number. Compare the original value with the reverse value. If they exactly matched, then it is a Palindrome number. Otherwise, it is not.
Python Palindrome Number Program using While Loop
This program allows the user to enter any integer value. Next, this program uses While Loop to check whether a given number is Palindrome or Not.
# using While Loop number = int(input("Please Enter any Value: ")) reverse = 0 temp = number while(temp > 0): Reminder = temp % 10 reverse = (reverse * 10) + Reminder temp = temp //10 print("Reverse of it is = %d" %reverse) if(number == reverse): print("%d is a Palindrome" %number) else: print("%d is Not" %number)

User Entered value in this palindrome program in Python are Number = 191 and Reverse = 0
Temp = Number
Temp = 191
While Loop First Iteration
Reminder = Temp %10
Reminder = 191 % 10 = 1
Reverse = Reverse *10 + Reminder => 0 * 10 + 1 = 1
Temp = Temp //10 = 191 /10
Temp = 19
Second Iteration: From the first while loop Iteration, the values changed as Temp = 19 and Reverse = 1
Reminder = 19 % 10 = 9
Reverse = 1 * 10 + 9 = 19
Temp = 19 /10
Temp = 1
Third Iteration: From the Second Iteration, the values of Temp = 1 and Reverse = 19
Reminder = 1 % 10 = 1
Reverse = 19 * 10 + 1 = 191
Temp = 1/10
Temp = 0
Here Num = 0. So, the Python while loop condition fails.
if ( Number == Reverse ) – condition checks whether the user entered number is exactly equal to the Reverse or not. If this condition is True, then it is. Else it is not.
Python Palindrome Number Program using Functions
In this program, we defined a function. Within that function, we used the If statement.
# using Functions def intRev(val): inverse = 0 while(val > 0): Remi = val % 10 inverse = (inverse * 10) + Remi val = val // 10 return inverse val = int(input("Please Enter any Num: ")) rev = intRev(val) print("Inverse = %d" %rev) if(val == rev): print("%d is a Palindrome" %val) else: print("%d is not" %val)
Please Enter any Num: 1441
Inverse = 1441
1441 is a Palindrome
Please Enter any Num: 1202
Inverse = 2021
1202 is not
Using recursion
In this Python program, we are checking whether a given number is Palindrome or Not using the Recursive Functions concept.
# using Recursive Functions rev = 0 def integer_rev(num): global rev if(num > 0): Reminder = num % 10 rev = (rev * 10) + Reminder integer_rev(num // 10) return rev num = int(input("Please Enter any Num: ")) rev = integer_rev(num) print("Reverse = %d" %rev) if(num == rev): print("%d is a Palindrome" %num) else: print("%d is not" %num)
Please Enter any Num: 4114
Reverse = 4114
4114 is a Palindrome
=================== RESTART: ===================
Please Enter any Num: 859
Reverse = 958
859 is not
In this program, within the checks using recursion function declaration,
integer_rev (Num//10) – it helps to call the function Recursively with the updated value. If you miss this statement, it terminates after completing the first line. For example, Num = 191, then the output is 1
Let’s see the If condition.
if (num > 0) check whether it is greater than 0 or not. For Recursive functions, placing a condition before using the function recursively is very important. Otherwise, we end up in infinite execution (Same as infinite Loop).
Python program to print Palindrome Numbers from 1 to N
maximum = int(input(" Please Enter the Maximum Value : ")) print("Between 1 and %d are : " %maximum) for num in range(1, maximum + 1): temp = num rv = 0 while(temp > 0): Reminder = temp % 10 rv = (rv * 10) + Reminder temp = temp //10 if(num == rv): print("%d " %num, end = ' ')
Please Enter the Maximum Value : 4554
Between 1 and 4554 are :
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 111 121 131 141 151 161 171 181 191 202 212 222 232 242 252 262 272 282 292 303 313 323 333 343 353 363 373 383 393 404 414 424 434 444 454 464 474 484 494 505 515 525 535 545 555 565 575 585 595 606 616 626 636 646 656 666 676 686 696 707 717 727 737 747 757 767 777 787 797 808 818 828 838 848 858 868 878 888 898 909 919 929 939 949 959 969 979 989 999 1001 1111 1221 1331 1441 1551 1661 1771 1881 1991 2002 2112 2222 2332 2442 2552 2662 2772 2882 2992 3003 3113 3223 3333 3443 3553 3663 3773 3883 3993 4004 4114 4224 4334 4444 4554