(adsbygoogle = window.adsbygoogle || []).push({ google_ad_client: "ca-pub-2960223314593660", enable_page_level_ads: true }); Python program to check whether a number is palindrome or not. - TecGlance

Header Ads

Python program to check whether a number is palindrome or not.

Algorithm:::



Step 1: Read a number, num
Step 2: Assign rev=0
Step 3: Assign temp=num
Step 4: Repeat steps 5,6&7 until num=0 reaches
Step 5: Compute rem=num%10
Step 6: Compute rev=rev*10+rem
Step 7: Compute num=num/10
Step 8: If rev==temp goto step 9 else goto steo 10
Step 9: Print number entered is a palindrome
Step 10: Print the number entered is not a palindrome
Step 11: Stop



Flowchart:::


Program code:::

num=int(input('Enter a number'))
rev=0
temp=num
while num>0:
    rem=num%10
    rev=rev*10+rem
    num=num/10
if rev==temp:
    print 'The number is a palindrome'
else:
    print 'The number is not a palindrome'
   

5 comments:

  1. I want it in a simple form can you help me please

    ReplyDelete
  2. I want it in a simple form can you help me please

    ReplyDelete
    Replies
    1. num = input('Enter any number : ')
      if num == str(num)[::-1]: //[::-1] is used for string reversal
      print('The number is a palindrome')
      else:
      print('The number is not a palindrome')

      Delete
  3. if the input is 100 then the reverse value according to your code is 1 but actually the reverse value 001.And you are comparing with the 100 and 1 which is wrong comparsion.so plz write the code such a way that 100 and 001 should be compared.

    ReplyDelete
    Replies
    1. Comparison is incorrect as you said. The system see's 001 as 1. so in order to get the comparison as 001 you will have to convert it into string. at the end it still is not a palindrome. so i think its not that serious issue to do the conversions and make the program a little more lengthy. If you have any other code for suggestion feel free to post it here. Im just trying to help others. My codes are not perfect bro.

      Delete

Powered by Blogger.