Divide two integers without using multiplication , division and mod operator | Set2 | C ++ 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 . ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Divide ( int a , int b ) { long long dividend = ( long long ) a ; long long divisor = ( long long ) b ; long long sign = ( dividend < 0 ) ^ ( divisor < 0 ) ? -1 : 1 ; dividend = abs ( dividend ) ; divisor = abs ( divisor ) ; if ( divisor == 0 ) { cout << " Cannot ▁ Divide ▁ by ▁ 0" << endl ; return ; } if ( dividend == 0 ) { cout << a << " ▁ / ▁ " << b << " ▁ is ▁ equal ▁ to ▁ : ▁ " << 0 << endl ; return ; } if ( divisor == 1 ) { cout << a << " ▁ / ▁ " << b << " ▁ is ▁ equal ▁ to ▁ : ▁ " << sign * dividend << endl ; return ; } cout << a << " ▁ / ▁ " << b << " ▁ is ▁ equal ▁ to ▁ : ▁ " << sign * exp ( log ( dividend ) - log ( divisor ) ) << endl ; } int main ( ) { int a = 10 , b = 5 ; Divide ( a , b ) ; a = 49 , b = -7 ; Divide ( a , b ) ; return 0 ; }