Divide two integers without using multiplication , division and mod operator | Set2 | Python3 program for above approach ; 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
import math NEW_LINE def Divide ( a , b ) : NEW_LINE INDENT dividend = a ; NEW_LINE divisor = b ; NEW_LINE sign = - 1 if ( ( dividend < 0 ) ^ ( divisor < 0 ) ) else 1 ; NEW_LINE dividend = abs ( dividend ) ; NEW_LINE divisor = abs ( divisor ) ; NEW_LINE if ( divisor == 0 ) : NEW_LINE INDENT print ( " Cannot ▁ Divide ▁ by ▁ 0" ) ; NEW_LINE DEDENT if ( dividend == 0 ) : NEW_LINE INDENT print ( a , " / " , b , " is ▁ equal ▁ to ▁ : " , 0 ) ; NEW_LINE DEDENT if ( divisor == 1 ) : NEW_LINE INDENT print ( a , " / " , b , " is ▁ equal ▁ to ▁ : " , ( sign * dividend ) ) ; NEW_LINE DEDENT print ( a , " / " , b , " is ▁ equal ▁ to ▁ : " , math . floor ( sign * math . exp ( math . log ( dividend ) - math . log ( divisor ) ) ) ) ; NEW_LINE DEDENT a = 10 ; NEW_LINE b = 5 ; NEW_LINE Divide ( a , b ) ; NEW_LINE a = 49 ; NEW_LINE b = - 7 ; NEW_LINE Divide ( a , b ) ; NEW_LINE