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)*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
// Java code to illustrate *, -, / using only// '+' arithmetic operatorclass 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
# 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#
// 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// 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?> |
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)*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
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...