Implement * , | Java 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
class GFG { static int flipSign ( int a ) { int neg = 0 ; int tmp = a < 0 ? 1 : - 1 ; while ( a != 0 ) { neg += tmp ; a += tmp ; } return neg ; } static boolean areDifferentSign ( int a , int b ) { return ( ( a < 0 && b > 0 ) || ( a > 0 && b < 0 ) ) ; } static int sub ( int a , int b ) { return a + flipSign ( b ) ; } static int mul ( int a , int b ) { if ( a < b ) return mul ( b , a ) ; int sum = 0 ; for ( int i = Math . abs ( b ) ; i > 0 ; i -- ) sum += a ; if ( b < 0 ) sum = flipSign ( sum ) ; return sum ; } static int division ( int a , int b ) { if ( b == 0 ) throw new ArithmeticException ( ) ; int quotient = 0 , dividend ; int divisor = flipSign ( Math . abs ( b ) ) ; for ( dividend = Math . abs ( a ) ; dividend >= Math . abs ( divisor ) ; dividend += divisor ) quotient ++ ; if ( areDifferentSign ( a , b ) ) quotient = flipSign ( quotient ) ; return quotient ; } public static void main ( String [ ] args ) { System . out . println ( " Subtraction ▁ is ▁ " + sub ( 4 , - 2 ) ) ; System . out . println ( " Product ▁ is ▁ " + mul ( - 9 , 6 ) ) ; try { System . out . println ( " Division ▁ is ▁ " + division ( 8 , 2 ) ) ; } catch ( ArithmeticException e ) { System . out . println ( " Exception ▁ : - ▁ Divide ▁ by ▁ 0" ) ; } } }