- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to check a number is palindrome or not without help of a string in Python
Suppose we have a non-negative integer called num, we have to check whether it is a palindrome or not. We have to solve it without using strings
So, if the input is like num = 25352, then the output will be True
To solve this, we will follow these steps −
a := 0
c := num
while num > 0, do
r := num mod 10
num := floor of num / 10
a :=(10 * a) + r
if a is same as c, then
return True
otherwise return False
Example
Let us see the following implementation to get better understanding
def solve(num): a = 0 c = num while num > 0: r = num % 10 num = num // 10 a = (10 * a) + r if a == c: return True else: return False num = 25352 print(solve(num))
Input
25352
Output
True
Advertisements