The Next Palindrome number

前端 未结 11 1746
走了就别回头了
走了就别回头了 2021-01-17 07:23

I am beginner in programming, So can you please tell me what\'s wrong with my code?

I want to print next palindrome number if the number entered by the user (n) is n

相关标签:
11条回答
  • 2021-01-17 07:44

    I have written this for finding next pallindrome number given a pallindrome number.

    def palindrome(num):
        bol=False
        #x=len(str(num))
        num=num+1
        while(bol==False):
            if(check_palindrome(num)):
                bol=True
            else:
                num=num+1
        return num
    def check_palindrome(n):
        temp=n
        rev=0
        while(n>0):
            dig=n%10
            rev=rev*10+dig
            n=n//10
        if(temp==rev):
            return True
    
    b=palindrome(8)
    print(b)
    
    0 讨论(0)
  • 2021-01-17 07:46

    This problem has a wonderful number of ways to solve them.

    One of them is

    def nearest_palindrome(number):
        #start writitng your code here
        while True:
            number+=1
            if str(number) == str(number)[::-1]:
                return number 
    
    number=12300
    print(nearest_palindrome(number))
    

    Thanks for your time to read my answer : )

    0 讨论(0)
  • 2021-01-17 07:46

    If a definite range is given:

    # function to check if the number is  a palindrome
    def palin(x):                 
        s=str(x)
        if s==s[::-1]:
            return True
        else:
            return False
    
    n=int(input("Enter the number"))
    # Putting up range from the next number till 15 digits
    for i in range(n+1,int(10e14)):
        if palin(i) is True:
            print(i)
            break
    
    0 讨论(0)
  • 2021-01-17 07:52

    using function to find next palindrome

    def nearest_palindrome(number):
    if number>0:
        i=1
        while(number):
            n=number+i
            temp=n
            sum=0
            while n>0:
                rem=n%10
                sum=sum*10+rem
                n=n//10
            if temp==sum:
    
                return temp
                #break
            else:
                sum=0
                i=i+1
            #continue
    number=12300
    print(nearest_palindrome(number))
    
    0 讨论(0)
  • 2021-01-17 07:52
    def nearest_palindrome(number):
    
        for i in range(1,number):
            number=number+1
            tem=str(number)
            tem1=tem[-1::-1]
            if(tem==tem1):
                return number
            else:
                continue
    
    number=12997979797979797
    
    print(nearest_palindrome(number))
    
    0 讨论(0)
提交回复
热议问题