Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
edited by

Hello, all. I'm totally new to the programming field, So can anyone tell me where I went wrong in this below code? 

I need to print the following palindrome number if the number entered by the client (n) isn't a palindrome 

n = int(input("Enter any number :- "))

reverse = 0

temp = n

while (n!=0):

    reverse = reverse * 10

    reverse = reverse + n%10

    n=n//10 

if(temp==reverse):

    print ("Already palindrome:: ")

if(temp != reverse):

     new_temp = temp

     new_reverse = 0

     for i in range(new_temp,new_temp+10):

        while(temp != 0):

            new_reverse = new_reverse * 10

            new_reverse = new_reverse + temp%10

            temp = temp//10

         if(new_temp==new_reverse):

             print ("Next pallindrome is :- ",new_temp)

             break

         if(new_temp != new_reverse):

             temp = new_temp+1

1 Answer

0 votes
by (26.4k points)
edited by

Here are two issues with your code. 

1) In your code, 

for i in range 

It will calculate the reverse of the temp variable, however, you don't change the temp variable's worth. You do 

new_temp = temp

 for i in range(new_temp,new_temp+10):

    [SNIP]

    if(new_temp != new_reverse):

         temp = new_temp+1 #this value never changes.

So you're making 10 emphasess with indeed the very same worth. 

2) Ten emphasess probably won't sufficiently be to discover a palindrome. Continue going until you discover a palindrome.

def reverse(num):

    reverse= 0

    while num:

        reverse= reverse*10 + num%10

        num= num//10

    return reverse

num= int(input("Enter any number :- "))

if num==reverse(num):

    print ("Already palindrome.")

else:

    while True:

        num+= 1

        if num==reverse(num):

            print ("Next palindrome is : %s"%num)

            break

Interested to learn python in detail? Join python course fast!

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...