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
< ? php function Divide ( $ a , $ b ) { $ dividend = $ a ; $ divisor = $ b ; $ sign = ( $ dividend < 0 ) ^ ( $ divisor < 0 ) ? -1 : 1 ; $ dividend = abs ( $ dividend ) ; $ divisor = abs ( $ divisor ) ; if ( $ divisor == 0 ) { echo " Cannot ▁ Divide ▁ by ▁ 0" ; echo " " ; } if ( $ dividend == 0 ) { echo $ a , " ▁ / ▁ " , $ b , " ▁ is ▁ equal ▁ to ▁ : ▁ " , 0 ; echo " " ; } if ( $ divisor == 1 ) { echo $ a , " ▁ / ▁ " , $ b , " ▁ is ▁ equal ▁ to ▁ : ▁ " , $ sign * $ dividend . " STRNEWLINE " ; echo " " ; } echo $ a , " ▁ / ▁ " , $ b , " ▁ is ▁ equal ▁ to ▁ : ▁ " , $ sign * exp ( log ( $ dividend ) - log ( $ divisor ) ) . " STRNEWLINE " ; echo " " ; } $ a = 10 ; $ b = 5 ; Divide ( $ a , $ b ) ; $ a = 49 ; $ b = -7 ; Divide ( $ a , $ b ) ; ? >