Implement * , | CPP code to illustrate * , - , / using only ' + ' arithmetic operator ; Function to flip the sign using only " + " operator ( It is simple with ' * ' allowed . We need to do a = ( - 1 ) * a ; If sign is + ve turn it - ve and vice - versa ; Check if a and b are of different signs ; Function to subtract two numbers by negating b and adding them ; Negating b ; Function to multiply a by b by adding a to itself b times ; because algo is faster if b < a ; Adding a to itself b times ; Check if final sign must be - ve or + ve ; Function to divide a by b by counting how many times ' b ' can be subtracted from ' a ' before getting 0 ; Raise exception if b is 0 ; Negating b to subtract from a ; Subtracting divisor from dividend ; Check if a and b are of similar symbols or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int flipSign ( int a ) { int neg = 0 ; int tmp = a < 0 ? 1 : -1 ; while ( a != 0 ) { neg += tmp ; a += tmp ; } return neg ; } bool areDifferentSign ( int a , int b ) { return ( ( a < 0 && b > 0 ) || ( a > 0 && b < 0 ) ) ; } int sub ( int a , int b ) { return a + flipSign ( b ) ; } int mul ( int a , int b ) { if ( a < b ) return mul ( b , a ) ; int sum = 0 ; for ( int i = abs ( b ) ; i > 0 ; i -- ) sum += a ; if ( b < 0 ) sum = flipSign ( sum ) ; return sum ; } int division ( int a , int b ) { if ( b == 0 ) throw ( b ) ; int quotient = 0 , dividend ; int divisor = flipSign ( abs ( b ) ) ; for ( dividend = abs ( a ) ; dividend >= abs ( divisor ) ; dividend += divisor ) quotient ++ ; if ( areDifferentSign ( a , b ) ) quotient = flipSign ( quotient ) ; return quotient ; } int main ( ) { cout << " Subtraction ▁ is ▁ " << sub ( 4 , -2 ) << endl ; cout << " Product ▁ is ▁ " << mul ( -9 , 6 ) << endl ; try { cout << " Division ▁ is ▁ " << division ( 8 , 2 ) ; } catch ( int k ) { cout << " ▁ Exception ▁ : - ▁ Divide ▁ by ▁ 0" ; } return 0 ; }