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
function truncMod ( a , n ) { let q = Math . round ( a / n ) ; return a - ( n * q ) ; } let a , b ; a = 9 ; b = 4 ; document . write ( a + " " + b + " " + truncMod ( a , b ) + " " ) ; a = - 9 ; b = 4 ; document . write ( a + " " + b + " " + truncMod ( a , b ) + " " ) ; a = 9 ; b = - 4 ; document . write ( a + " " + b + " " + truncMod ( a , b ) + " " ) ; a = - 9 ; b = - 4 ; document . write ( a + " " + b + " " + truncMod ( a , b ) + " " ) ;