Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Palindrome Program in Python

by suresh

Write a Palindrome Program in Python using While Loop, Functions, and Recursion. Any number could be Palindrome in python if it remained the same when we reversed it. For example, 131 is a palindrome number because this number remains the same after reversing it.

The following steps show the common approach to check for the Python Palindrome Number.

  1. Enter any number
  2. Reverse a given number
  3. Compare the original value with reverse value.
  4. If they exactly matched, then it is a Python Palindrome number. Otherwise, it is not a Palindrome number.

Python Palindrome Program using While Loop

This Palindrome program in Python allows the user to enter any positive integer. Next, this Python palindrome program use While Loop to check whether a given number is Palindrome Number or Not.

# 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)
Palindrome Program in Python 1

User Entered value in this palindrome program in Python are Number = 191 and Reverse = 0
Temp = Number
Temp = 191

Python While Loop First Iteration
Reminder = Temp %10
Reminder = 191 % 10 = 1

Reverse = Reverse *10 + Reminder = 0 * 10 + 1
Reverse = 0 + 1 = 1

Temp = Temp //10 = 191 /10
Temp = 19

Second Iteration
From the first Python while loop Iteration, the values of both Temp and Reverse changed as Temp = 19 and Reverse = 1

Reminder = 19 % 10 = 9

Reverse = 1 * 10 + 9 = 10 + 9
Reverse = 19

Temp = 19 /10
Temp = 1

Third Iteration
From the second Iteration of palindrome program in Python, the values of 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 fails.

if ( Number == Reverse ) – condition checks whether the user enter number is exactly equal to the 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

In this Python palindrome program, we defined a function. Within that function, we used the If statement.

# 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 2

Palindrome Program using Recursion

In this Python Palindrome program, we are checking whether a given number is Palindrome Number or Not using the 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 = '  ')
Palindrome Program in Python 3

In this palindrome program, within the function declaration,

integer_reverse (Number // 10) – it helps to call the function Recursively with the updated value. If you miss this statement, then after completing the first line, it terminates. For example, Number = 191 then the output is 1

Let’s see the If condition.

if (Number > 0) 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 end up in infinite execution (Same like infinite Loop).

Placed Under: Python Examples

  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy