Python Program to Check if a Number is a Palindrome
Aim:
Algorithm of Palindrome Number
- step: Start
- step: Declare the variable n and read n
- step: Assign n = k
- step: Initialise r = 0
- step: In a while, loop perform
- d = n % 10
- r = r * 10 + d
- n = math.floor(n / 10)
- step: check the condition r == k
- step: If the condition is a true print that given number is a palindrome
- step: Otherwise print the given number is not a palindrome
- 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
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:
- Step: Start
- Step: Take user input as string: string = input(“Enter a String:)
- Check if(string == string[ : :-1])
- If True then print (” Palindrome “)
- 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
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:
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find the common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number
Get Salesforce Answers
where to buy dostinex without a prescription – dostinex 0.25mg usa order alesse