-3
#WAP to check given number is Armstrong or not, (done)
#if it is Armstrong then print reverse of that number,  (done)
#if it is not Armstrong then check it is Palindrome or not.   (problem)

no=int(input("Enter your number:"))
temp=no
arm=0
rev=0
while(no>0):
    rem=no%10
    cube=rem*rem*rem
    arm=arm+cube
    no=no//10

if(temp==arm):
    while (temp> 0):
        rem = temp % 10
        rev = (rev * 10) + rem
        temp = temp // 10
    print("Reverse is:", rev)

elif(temp!=arm):
    while (temp > 0):
        rem = temp % 10
        rev = rev * 10 + rem
        temp = temp // 10

if(rev==temp):
    print("It's a palindrome.")

else:
    print("It's not a palindrome.")

I can't find out the problem with the "check if it is a palindrome" part.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
petergorrr
  • 29
  • 3
  • 3
    Does this answer your question? [How to check for palindrome using Python logic](https://stackoverflow.com/questions/17331290/how-to-check-for-palindrome-using-python-logic) – theProcrastinator Mar 11 '21 at 05:13

2 Answers2

1

In your code to check for palindrome, you are repeatedly dividing your temp value by 10, but your condition is for temp>0 which will never be reached, as repeated division by 10 will never result in a negative number. So, you should change your condition to while(temp>=1).

Also, you should compare the final value of rev to no instead of with temp. So if you change your final condition to if(rev==no): it should work. This is because your temp keeps getting modified in your loop to check for palindrome, whereas you want to compare your final rev with the original number.

Arijit Gupta
  • 73
  • 2
  • 8
  • Hi Arijit, thank you for your comment but unfortunately I tried and that doesn't work.... the codes still failed to check palindrome. – petergorrr Mar 13 '21 at 06:14
0

Try this to check if number is palindrome or not

if str(temp)==str(temp)[::-1]:
    print("Number is palindrome")
else:
    print("Not palindrome")
SAI SANTOSH CHIRAG
  • 2,046
  • 1
  • 10
  • 26