Modulo Operations in Programming With Negative Results | Function to calculate and return the remainder of a % n ; Type casting is necessary as ( int ) / ( int ) will give int result , i . e . - 3 / 2 will give - 1 and not - 1.5 ; Return the resultant remainder ; 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 floorMod ( a , n ) { let q = Math . floor ( Math . floor ( a / n ) ) ; return a - n * q ; } let a , b ; a = 9 ; b = 4 ; document . write ( a + " " + b + " " + floorMod ( a , b ) + " " ) ; a = - 9 ; b = 4 ; document . write ( a + " " + b + " " + floorMod ( a , b ) + " " ) ; a = 9 ; b = - 4 ; document . write ( a + " " + b + " " + floorMod ( a , b ) + " " ) ; a = - 9 ; b = - 4 ; document . write ( a + " " + b + " " + floorMod ( a , b ) + " " ) ;