Smallest number to be subtracted to convert given number to a palindrome | Function to evaluate minimum subtraction required to make a number palindrome ; Counts number of subtractions required ; Run a loop till N >= 0 ; Store the current number ; Store reverse of current number ; Reverse the number ; Check if N is palindrome ; Increment the counter ; Reduce the number by 1 ; Print the result ; Driver Code ; Function call
def minSub ( N ) : NEW_LINE INDENT count = 0 NEW_LINE while ( N >= 0 ) : NEW_LINE INDENT num = N NEW_LINE rev = 0 NEW_LINE while ( num != 0 ) : NEW_LINE INDENT digit = num % 10 NEW_LINE rev = ( rev * 10 ) + digit NEW_LINE num = num // 10 NEW_LINE DEDENT if ( N == rev ) : NEW_LINE INDENT break NEW_LINE DEDENT count += 1 NEW_LINE N -= 1 NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3456 NEW_LINE minSub ( N ) NEW_LINE DEDENT