Palindrome Number in Python with Algorithm

Python Program to Check if a Number is a Palindrome

Aim:

To check given number is palindrome or not. If it is palindrome then print palindrome or not then print non-palindrome.
 

Algorithm of Palindrome Number

  1. step: Start
  2. step: Declare the variable n and read n
  3. step: Assign n = k
  4. step: Initialise r = 0 
  5. step: In a while, loop  perform 
    1. d = n % 10
    2. r = r * 10 + d
    3. n = math.floor(n / 10)
  6. step: check the condition r == k
  7. step: If the condition is a true print that given number is a palindrome
  8. step: Otherwise print the given number is not a palindrome
  9. step: end

Palindrome Number in Python Source Code:

import math
n = int(input('enter the number :'))
k = n
r = 0
while n > 0:
d =n % 10
r = r * 10 + d
n = math.floor(n/10)
if r == k:
print('given number is palindrom ')
else:
print('given number is not palindrome')

Output: 

Input as a number: 1212

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
enter the number :1212
given number is not palindrome

Process finished with exit code 0
Input as a number: 12345
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
enter the number :12345
given number is not palindrome

Process finished with exit code 0

How To Check String is Palindrome or Not

In this program, we will check the given string is Palindrome or not

Algorithm:

  1. Step: Start
  2. Step: Take user input as string: string = input(“Enter a String:)
  3. Check if(string == string[ : :-1])
  4. If True then print (” Palindrome “)
  5. Else: Print (” Not Palindrome “)

Source Code to Check String is Palindrome or Not:

string1 = input("Enter any String: ")
if(string1 == string1[::-1]): #[::-1] to compare string from latst
print('The given string is a palindrome')
else:
print('Given String is Not a palindrome')

Output:

Input as a string: madam

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Enter any String: madam
The given string is a palindrome

Process finished with exit code 0
Input as a string: Pramod
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Enter any String: Pramod
Given String is Not a palindrome

Process finished with exit code 0

FAQ:

What is palindrome string in Python?

A palindrome string is nothing but an input string and the reversal of that string is the same as the input string. Example: A reversed string of madam is madam only so we can say that madam string is a palindrome.

How do you check if a given string is a palindrome in Python?

To check given string is palindrome or not that we have to reverse the string and compare it with the input string. If both strings are the same then we can say palindrome. Example: string1 = input(“Enter any String: “)
if(string1 == string1[::-1]):
print(‘The given string is a palindrome’)
else:
print(‘Given String is Not a palindrome’)

How do you find a palindrome number?

To check the given number is palindrome or not that we can check like this:
import math
n = int(input(‘enter the number :’))
k = n
r = 0
while n > 0:
d =n % 10
r = r * 10 + d
n = math.floor(n/10)
if r == k:
print(‘ palindrom ‘)
else
:print(‘not palindrome’)

Recommended Post:

Get Salesforce Answers

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment