3

Accept a number as an input and check whether the given number is palindrome or not if it is a palindrome number print the number on the screen if it is not a palindrome number reverse that number and add it to previous number repeat this until will get a palindrome number and print that palindrome number on the screen

input: 127

output: 848

I will be thank full if any one can solve this:

    n = int(input())
    if str(n) == str(n)[::-1]:
        print(str(n)[::-1],"is a palindrome")
    else:

what should i write in else condition

venkat0107
  • 240
  • 1
  • 12

2 Answers2

0

This works for me:

n = int(input())
while True :
    if str(n) == str(n)[::-1]:
        print(str(n)[::-1],"is a palindrome")
        break
    else:
        n += int(str(n)[::-1])
lenik
  • 23,228
  • 4
  • 34
  • 43
  • I need code for **if it is not a palindrome number reverse that number and add it to previous number repeat this until will get a palindrome number and print that palindrome number on the screen** – venkat0107 May 28 '20 at 14:32
0

A mathematical solution — without converting to string — would be

def revert(x: int) -> int:
    x = abs(x)
    r = 0
    while x:
        x, m = divmod(x, 10)
        r = r * 10 + m
    return r
def findPalindrome(x: int) -> bool:
    if x < 0:
        return 0
    r = revert(x)
    if x == r:
        return x
    else:
        return findPalindrome(x + r)
kafran
  • 729
  • 7
  • 13