K | Function to find the kth digit from last in an eger n ; If k is less than equal to 0 ; Divide the number n by 10 upto k - 1 times ; If the number n is equal 0 ; Pr the right most digit ; Driver code ; Given Input ; Function call
def kthDigitFromLast ( n , k ) : NEW_LINE INDENT if ( k <= 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT while ( ( k - 1 ) > 0 and n > 0 ) : NEW_LINE INDENT n = n / 10 NEW_LINE k -= 1 NEW_LINE DEDENT if ( n == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( int ( n % 10 ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 2354 NEW_LINE k = 2 NEW_LINE kthDigitFromLast ( n , k ) NEW_LINE DEDENT