Implement *, – and / operations using only + arithmetic operator
Given two numbers, perform multiplication, subtraction, and division operations on them, using ‘+’ arithmetic operator only.
Operations can be performed as follows:
Subtraction :- a - b = a + (-1)*b. Multiplication :- a * b = a + a + a ... b times. Division :- a / b = continuously subtract b from a and count how many times we can do that.
The above steps look simple, but it is slightly challenging as we can’t even use – to subtract.
C++
// CPP code to illustrate *, -, / using only // '+' arithmetic operator #include <bits/stdc++.h> using namespace std; // Function to flip the sign using only "+" // operator (It is simple with '*' allowed. // We need to do a = (-1)*a int flipSign( int a) { int neg = 0; // If sign is + ve turn it -ve // and vice-versa int tmp = a < 0 ? 1 : -1; while (a != 0) { neg += tmp; a += tmp; } return neg; } // Check if a and b are of different signs bool areDifferentSign( int a, int b) { return ((a<0 && b> 0) || (a > 0 && b < 0)); } // Function to subtract two numbers // by negating b and adding them int sub( int a, int b) { // Negating b return a + flipSign(b); } // Function to multiply a by b by // adding a to itself b times int mul( int a, int b) { // because algo is faster if b<a if (a < b) return mul(b, a); // Adding a to itself b times int sum = 0; for ( int i = abs (b); i > 0; i--) sum += a; // Check if final sign must be -ve or + ve if (b < 0) sum = flipSign(sum); return sum; } // Function to divide a by b by counting how many // times 'b' can be subtracted from 'a' before // getting 0 int division( int a, int b) { // Raise exception if b is 0 if (b == 0) throw (b); int quotient = 0, dividend; // Negating b to subtract from a int divisor = flipSign( abs (b)); // Subtracting divisor from dividend for (dividend = abs (a); dividend >= abs (divisor); dividend += divisor) quotient++; // Check if a and b are of similar symbols or not if (areDifferentSign(a, b)) quotient = flipSign(quotient); return quotient; } // Driver code 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; } |
Java
// Java code to illustrate *, -, / using only // '+' arithmetic operator class GFG{ // Function to flip the sign using only "+" // operator (It is simple with '*' allowed. // We need to do a = (-1)*a static int flipSign( int a) { int neg = 0 ; // If sign is + ve turn it -ve // and vice-versa int tmp = a < 0 ? 1 : - 1 ; while (a != 0 ) { neg += tmp; a += tmp; } return neg; } // Check if a and b are of different signs static boolean areDifferentSign( int a, int b) { return ((a < 0 && b > 0 ) || (a > 0 && b < 0 )); } // Function to subtract two numbers // by negating b and adding them static int sub( int a, int b) { // Negating b return a + flipSign(b); } // Function to multiply a by b by // adding a to itself b times static int mul( int a, int b) { // because algo is faster if b<a if (a < b) return mul(b, a); // Adding a to itself b times int sum = 0 ; for ( int i = Math.abs(b); i > 0 ; i--) sum += a; // Check if final sign must be -ve or + ve if (b < 0 ) sum = flipSign(sum); return sum; } // Function to divide a by b by counting // how many times 'b' can be subtracted // from 'a' before getting 0 static int division( int a, int b) { // Raise exception if b is 0 if (b == 0 ) throw new ArithmeticException(); int quotient = 0 , dividend; // Negating b to subtract from a int divisor = flipSign(Math.abs(b)); // Subtracting divisor from dividend for (dividend = Math.abs(a); dividend >= Math.abs(divisor); dividend += divisor) quotient++; // Check if a and b are of similar symbols or not if (areDifferentSign(a, b)) quotient = flipSign(quotient); return quotient; } // Driver code 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" ); } } } // This code is contributed by mits |
Python3
# Python3 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 def flipSign(a): neg = 0 ; # If sign is + ve turn it -ve # and vice-versa tmp = 1 if a < 0 else - 1 ; while (a ! = 0 ): neg + = tmp; a + = tmp; return neg; # Check if a and b are of different signs def areDifferentSign(a, b): return ((a < 0 and b > 0 ) or (a > 0 and b < 0 )); # Function to subtract two numbers # by negating b and adding them def sub(a, b): # Negating b return a + flipSign(b); # Function to multiply a by b by # adding a to itself b times def mul(a, b): # because algo is faster if b<a if (a < b): return mul(b, a); # Adding a to itself b times sum = 0 ; for i in range ( abs (b), 0 , - 1 ): sum + = a; # Check if final sign must # be -ve or + ve if (b < 0 ): sum = flipSign( sum ); return sum ; # Function to divide a by b by counting # how many times 'b' can be subtracted # from 'a' before getting 0 def division(a, b): quotient = 0 ; # Negating b to subtract from a divisor = flipSign( abs (b)); # Subtracting divisor from dividend for dividend in range ( abs (a), abs (divisor) + divisor, divisor): quotient + = 1 ; # Check if a and b are of similar # symbols or not if (areDifferentSign(a, b)): quotient = flipSign(quotient); return quotient; # Driver code print ( "Subtraction is" , sub( 4 , - 2 )); print ( "Product is" , mul( - 9 , 6 )); a, b = 8 , 2 ; if (b): print ( "Division is" , division(a, b)); else : print ( "Exception :- Divide by 0" ); # This code is contributed by mits |
C#
// C# code to illustrate *, -, / using only // '+' arithmetic operator using System; class GFG { // Function to flip the sign using only "+" // operator (It is simple with '*' allowed. // We need to do a = (-1)*a static int flipSign( int a) { int neg = 0; // If sign is + ve turn it -ve // and vice-versa int tmp = a < 0 ? 1 : -1; while (a != 0) { neg += tmp; a += tmp; } return neg; } // Check if a and b are of different signs static bool areDifferentSign( int a, int b) { return ((a < 0 && b > 0) || (a > 0 && b < 0)); } // Function to subtract two numbers // by negating b and adding them static int sub( int a, int b) { // Negating b return a + flipSign(b); } // Function to multiply a by b by // adding a to itself b times static int mul( int a, int b) { // because algo is faster if b<a if (a < b) return mul(b, a); // Adding a to itself b times int sum = 0; for ( int i = Math.Abs(b); i > 0; i--) sum += a; // Check if final sign must be -ve or + ve if (b < 0) sum = flipSign(sum); return sum; } // Function to divide a by b by counting how many // times 'b' can be subtracted from 'a' before // getting 0 static int division( int a, int b) { // Raise exception if b is 0 if (b == 0) throw new ArithmeticException(); int quotient = 0, dividend; // Negating b to subtract from a int divisor = flipSign(Math.Abs(b)); // Subtracting divisor from dividend for (dividend = Math.Abs(a); dividend >= Math.Abs(divisor); dividend += divisor) quotient++; // Check if a and b are of similar symbols or not if (areDifferentSign(a, b)) quotient = flipSign(quotient); return quotient; } // Driver code public static void Main() { Console.WriteLine( "Subtraction is " + sub(4, -2)); Console.WriteLine( "Product is " + mul(-9, 6)); try { Console.WriteLine( "Division is " + division(8, 2)); } catch (Exception) { Console.WriteLine( "Exception :- Divide by 0" ); } } } //This code is contributed by mits |
PHP
<?php // PHP 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 function flipSign( $a ) { $neg = 0; // If sign is + ve turn it -ve // and vice-versa $tmp = $a < 0 ? 1 : -1; while ( $a != 0) { $neg += $tmp ; $a += $tmp ; } return $neg ; } // Check if a and b are of different signs function areDifferentSign( $a , $b ) { return (( $a < 0 && $b > 0) || ( $a > 0 && $b < 0)); } // Function to subtract two numbers // by negating b and adding them function sub( $a , $b ) { // Negating b return $a + flipSign( $b ); } // Function to multiply a by b by // adding a to itself b times function mul( $a , $b ) { // because algo is faster if b<a if ( $a < $b ) return mul( $b , $a ); // Adding a to itself b times $sum = 0; for ( $i = abs ( $b ); $i > 0; $i --) $sum += $a ; // Check if final sign must be // -ve or + ve if ( $b < 0) $sum = flipSign( $sum ); return $sum ; } // Function to divide a by b by counting // how many times 'b' can be subtracted // from 'a' before getting 0 function division( $a , $b ) { $quotient = 0; // Negating b to subtract from a $divisor = flipSign( abs ( $b )); // Subtracting divisor from dividend for ( $dividend = abs ( $a ); $dividend >= abs ( $divisor ); $dividend += $divisor ) $quotient ++; // Check if a and b are of similar // symbols or not if (areDifferentSign( $a , $b )) $quotient = flipSign( $quotient ); return $quotient ; } // Driver code print ( "Subtraction is " . sub(4, -2) . "\n" ); print ( "Product is " . mul(-9, 6) . "\n" ); list( $a , $b ) = array (8, 2); if ( $b ) print ( "Division is " . division( $a , $b )); else print ( "Exception :- Divide by 0" ); // This code is contributed by mits ?> |
Javascript
<script> // JavaScript 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 function flipSign(a) { var neg = 0; // If sign is + ve turn it -ve // and vice-versa var tmp = a < 0 ? 1 : -1; while (a != 0) { neg += tmp; a += tmp; } return neg; } // Check if a and b are of different signs function areDifferentSign(a , b) { return ((a < 0 && b > 0) || (a > 0 && b < 0)); } // Function to subtract two numbers // by negating b and adding them function sub(a , b) { // Negating b return a + flipSign(b); } // Function to multiply a by b by // adding a to itself b times function mul(a , b) { // because algo is faster if b<a if (a < b) return mul(b, a); // Adding a to itself b times var sum = 0; for (i = Math.abs(b); i > 0; i--) sum += a; // Check if final sign must be -ve or + ve if (b < 0) sum = flipSign(sum); return sum; } // Function to divide a by b by counting // how many times 'b' can be subtracted // from 'a' before getting 0 function division(a , b) { // Raise exception if b is 0 if (b == 0) throw new ArithmeticException(); var quotient = 0, dividend; // Negating b to subtract from a var divisor = flipSign(Math.abs(b)); // Subtracting divisor from dividend for (dividend = Math.abs(a); dividend >= Math.abs(divisor); dividend += divisor) quotient++; // Check if a and b are of similar symbols or not if (areDifferentSign(a, b)) quotient = flipSign(quotient); return quotient; } // Driver code document.write( "Subtraction is " + sub(4, -2)); document.write( "<br>Product is " + mul(-9, 6)); try { document.write( "<br>Division is " + division(8, 2)); } catch (e) { document.write( "Exception :- Divide by 0" ); } // This code is contributed by Amit Katiyar </script> |
Output:
Subtraction is 6 Product is -54 Division is 4
Time Complexity: O(max(|a|, |b|)), Where flipSign() function is O(|a|), sub() function is O(|b|), mul() function is O(max(|a|, |b|)) and division() function is O(|a/b|), Thus Overall, the time complexity of the code is O(max(|a|, |b|)).
Space Complexity: O(1), as it does not use any additional data structures.
Related Articles :
- Add two numbers without using arithmetic operators
- Subtract two numbers without using arithmetic operators
- Multiply two integers without using multiplication, division and bitwise operators, and no loops
This article is contributed by Sakshi Tiwari. If you like GeeksforGeeks (We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...