Palindrome Program in Python using While Loop

We will develop a palindrome program in python using while loop. It will check if the given number is a palindrome number or not. If the Reverse of a number is equal to the same number then the number is called a palindrome number.

Example of palindrome number:-
22 = 22 So, 22 is a palindrome number.
13 != 31 So, 13 is not a palindrome number.

This program completely depends on the program to find the reverse of a number. After finding the reverse of a number, compare the result and the actual number if both are the same then the given number is a palindrome number else the number is not a palindrome number.

Prerequisite:- Python program to find reverse of a number

Palindrome Number in Python using While Loop

We will take integer numbers while declaring the variables. Then, find the reverse of a number using the while loop and check if the number is equal to the reverse number or not using the if-else statement. Finally, the result will be displayed on the screen.

Program Description:- Write a Python program to check if the number is a palindrome or not using while loop

# palindrome program in python using while loop

# take inputs
num = 11
# calculate reverse of number
reverse = 0
number = num
while(num != 0):
   remainder = num % 10
   reverse = reverse * 10 + remainder
   num = int(num / 10)

# compare reverse to original number
if(number == reverse):
   print(number,'is a Palindrome')
else:
   print(number,'is not a Palindrome')

Output:- 

11 is a Palindrome

Palindrome Program in Python using While Loop

In the previous program, inputs are hardcoded in the program but in this program, input will be provided by the user.

Program Description:- Write a program to check if the given number is a palindrome or not in Python using while loop

# palindrome number in python using while loop

# take inputs
num = int(input('Enter the number: '))

# calculate reverse of number
reverse = 0
number = num
while(num != 0):
   remainder = num % 10
   reverse = reverse * 10 + remainder
   num = int(num / 10)

# compare reverse to original number
if(number == reverse):
   print(number,'is a Palindrome')
else:
   print(number,'is not a Palindrome')

Output for the input values test-case-1:-

Enter the number: 55
55 is a Palindrome

Output for the input values test-case-2:-

Enter the number: 125
125 is not a Palindrome

Palindrome Number Program in Python using While Loop

We can also take the help of a function to check the palindrome number in python. A function is a block of code that performs a specific task.

# Palindrome program in python using while loop

def isPalindrome(n):  #user-defined function
    # calculate reverse of number
    reverse = 0
    reminder = 0
    while(n != 0):
        remainder = n % 10
        reverse = reverse * 10 + remainder
        n = int(n / 10)
    return reverse

# take inputs
num = 55
# calling function and display result
reverse = isPalindrome(num)
if(num == reverse):
  print(num,'is a Palindrome')
else:
  print(num,'is not a Palindrome')

Output:-

Enter the number: 27572
27572 is a Palindrome

Leave a Comment

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