Implement *, – and / operations using only + arithmetic operator

19 May, 2021
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++
Java
Python3
C#
PHP
Javascript
// 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)*aint 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 signsbool areDifferentSign(int a, int b){    return ((a<0 && b> 0) || (a > 0 && b < 0));} // Function to subtract two numbers// by negating b and adding themint sub(int a, int b){    // Negating b    return a + flipSign(b);} // Function to multiply a by b by// adding a to itself b timesint 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 0int 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 codeint 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 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)*astatic 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 signsstatic 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 themstatic int sub(int a, int b){    // Negating b    return a + flipSign(b);} // Function to multiply a by b by// adding a to itself b timesstatic 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 0static 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 codepublic 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 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)*adef 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 signsdef 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 themdef sub(a, b):     # Negating b    return a + flipSign(b); # Function to multiply a by b by# adding a to itself b timesdef 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 0def 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 codeprint("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# code to illustrate *, -, / using only// '+' arithmetic operatorusing System;class GFG{// Function to flip the sign using only "+"// operator (It is simple with '*' allowed.// We need to do a = (-1)*astatic 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 signsstatic 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 themstatic int sub(int a, int b){    // Negating b    return a + flipSign(b);} // Function to multiply a by b by// adding a to itself b timesstatic 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 0static 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 codepublic 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 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)*afunction 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 signsfunction areDifferentSign($a, $b){    return (($a < 0 && $b > 0) ||            ($a > 0 && $b < 0));} // Function to subtract two numbers// by negating b and adding themfunction sub($a, $b){    // Negating b    return $a + flipSign($b);} // Function to multiply a by b by// adding a to itself b timesfunction 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 0function 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 codeprint("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?>
<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)*afunction 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 signsfunction areDifferentSign(a , b){    return ((a < 0 && b > 0) || (a > 0 && b < 0));} // Function to subtract two numbers// by negating b and adding themfunction sub(a , b){    // Negating b    return a + flipSign(b);} // Function to multiply a by b by// adding a to itself b timesfunction 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 0function 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
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 contribute@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. 
Mithun Kumar
amit143katiyar
anikaseth98
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Merge two sorted arrays with O(1) extra space
Program to print prime numbers from 1 to N.
Find next greater number with same set of digits
Segment Tree | Set 1 (Sum of given range)
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java