Program to check a number is palindrome or not without help of a string in Python

PythonServer Side ProgrammingProgramming

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
raja
Published on 12-Oct-2021 11:33:02
Advertisements