Modulo Operations in Programming With Negative Results | Function to calculate and return the remainder of a % n ; ( a / n ) implicitly gives the truncated result ; Driver Code ; Modulo of two positive numbers ; Modulo of a negative number by a positive number ; Modulo of a positive number by a negative number ; Modulo of two negative numbers
def truncMod ( a , n ) : NEW_LINE INDENT q = a // n NEW_LINE return a - n * q NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 9 NEW_LINE b = 4 NEW_LINE print ( a , " % " , b , " = " , truncMod ( a , b ) ) NEW_LINE a = - 9 NEW_LINE b = 4 NEW_LINE print ( a , " % " , b , " = " , truncMod ( a , b ) ) NEW_LINE a = 9 NEW_LINE b = - 4 NEW_LINE print ( a , " % " , b , " = " , truncMod ( a , b ) ) NEW_LINE a = - 9 NEW_LINE b = - 4 NEW_LINE print ( a , " % " , b , " = " , truncMod ( a , b ) ) NEW_LINE DEDENT