Divide two integers without using multiplication , division and mod operator | Set2 | Returns the quotient of dividend / divisor . ; Calculate sign of divisor i . e . , sign will be negative only if either one of them is negative otherwise it will be positive ; Remove signs of dividend and divisor ; Zero division Exception . ; Using Formula derived above . ; Driver Code
function Divide ( a , b ) { var dividend = a ; var divisor = b ; var sign = ( dividend < 0 ) ^ ( divisor < 0 ) ? - 1 : 1 ; dividend = Math . abs ( dividend ) ; divisor = Math . abs ( divisor ) ; if ( divisor == 0 ) { document . write ( " " ) ; return ; } if ( dividend == 0 ) { document . write ( a + " " + b + " " + 0 + " " ) ; return ; } if ( divisor == 1 ) { System . out . println ( a + " " + b + " " + sign * dividend + " " ) ; return ; } document . write ( a + " " + b + " " + Math . floor ( sign * ( Math . exp ( Math . log ( dividend ) - Math . log ( divisor ) ) ) ) + " " ) ; } var a = 10 , b = 5 ; Divide ( a , b ) ; a = 49 ; b = - 7 ; Divide ( a , b ) ;