Implement *, – and / operations using only + arithmetic operator
Last Updated :
23 Mar, 2023
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++
#include <bits/stdc++.h>
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;
}
|
Java
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" );
}
}
}
|
Python3
def flipSign(a):
neg = 0 ;
tmp = 1 if a < 0 else - 1 ;
while (a ! = 0 ):
neg + = tmp;
a + = tmp;
return neg;
def areDifferentSign(a, b):
return ((a < 0 and b > 0 ) or
(a > 0 and b < 0 ));
def sub(a, b):
return a + flipSign(b);
def mul(a, b):
if (a < b):
return mul(b, a);
sum = 0 ;
for i in range ( abs (b), 0 , - 1 ):
sum + = a;
if (b < 0 ):
sum = flipSign( sum );
return sum ;
def division(a, b):
quotient = 0 ;
divisor = flipSign( abs (b));
for dividend in range ( abs (a),
abs (divisor) + divisor,
divisor):
quotient + = 1 ;
if (areDifferentSign(a, b)):
quotient = flipSign(quotient);
return quotient;
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" );
|
C#
using System;
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 bool 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()
{
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" );
}
}
}
|
PHP
<?php
function flipSign( $a )
{
$neg = 0;
$tmp = $a < 0 ? 1 : -1;
while ( $a != 0)
{
$neg += $tmp ;
$a += $tmp ;
}
return $neg ;
}
function areDifferentSign( $a , $b )
{
return (( $a < 0 && $b > 0) ||
( $a > 0 && $b < 0));
}
function sub( $a , $b )
{
return $a + flipSign( $b );
}
function mul( $a , $b )
{
if ( $a < $b )
return mul( $b , $a );
$sum = 0;
for ( $i = abs ( $b ); $i > 0; $i --)
$sum += $a ;
if ( $b < 0)
$sum = flipSign( $sum );
return $sum ;
}
function division( $a , $b )
{
$quotient = 0;
$divisor = flipSign( abs ( $b ));
for ( $dividend = abs ( $a );
$dividend >= abs ( $divisor );
$dividend += $divisor )
$quotient ++;
if (areDifferentSign( $a , $b ))
$quotient = flipSign( $quotient );
return $quotient ;
}
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" );
?>
|
Javascript
<script>
function flipSign(a)
{
var neg = 0;
var tmp = a < 0 ? 1 : -1;
while (a != 0)
{
neg += tmp;
a += tmp;
}
return neg;
}
function areDifferentSign(a , b)
{
return ((a < 0 && b > 0) || (a > 0 && b < 0));
}
function sub(a , b)
{
return a + flipSign(b);
}
function mul(a , b)
{
if (a < b)
return mul(b, a);
var sum = 0;
for (i = Math.abs(b); i > 0; i--)
sum += a;
if (b < 0)
sum = flipSign(sum);
return sum;
}
function division(a , b)
{
if (b == 0)
throw new ArithmeticException();
var quotient = 0, dividend;
var 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;
}
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" );
}
</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 :
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...