Product of N with its largest odd digit | Function to return the largest odd digit in n ; If all digits are even then - 1 will be returned ; Last digit from n ; If current digit is odd and > maxOdd ; Remove last digit ; Return the maximum odd digit ; Function to return the product of n with its largest odd digit ; If there are no odd digits in n ; Product of n with its largest odd digit ; Driver code
def largestOddDigit ( n ) : NEW_LINE INDENT maxOdd = - 1 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT digit = n % 10 NEW_LINE if ( digit % 2 == 1 and digit > maxOdd ) : NEW_LINE INDENT maxOdd = digit NEW_LINE DEDENT n = n // 10 NEW_LINE DEDENT return maxOdd NEW_LINE DEDENT def getProduct ( n ) : NEW_LINE INDENT maxOdd = largestOddDigit ( n ) NEW_LINE if ( maxOdd == - 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return ( n * maxOdd ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 12345 NEW_LINE print ( getProduct ( n ) ) NEW_LINE DEDENT