Smaller palindromic number closest to N | Function to check if a number is palindrome or not ; Stores reverse of N ; Stores the value of N ; Calculate reverse of N ; Update rev ; Update N ; Update N ; If N is equal to rev of N ; Function to find the closest smaller palindromic number to N ; Calculate closest smaller palindromic number to N ; Update N ; Driver Code
def checkPalindrome ( N ) : NEW_LINE INDENT rev = 0 NEW_LINE temp = N NEW_LINE while ( N != 0 ) : NEW_LINE INDENT rev = rev * 10 + N % 10 NEW_LINE N = N // 10 NEW_LINE DEDENT N = temp NEW_LINE if ( N == rev ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def closestSmallerPalindrome ( N ) : NEW_LINE INDENT while N >= 0 and not checkPalindrome ( N ) : NEW_LINE INDENT N -= 1 NEW_LINE DEDENT return N NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4000 NEW_LINE print ( closestSmallerPalindrome ( N ) ) NEW_LINE DEDENT