Multiply two integers without using multiplication, division and bitwise operators, and no loops

08 Jul, 2022
By making use of recursion, we can multiply two integers with the given constraints. To multiply x and y, recursively add x y times.  
Approach:
Since we cannot use any of the given symbols, the only way left is to use recursion, with the fact that x is to be added to x y times.
Base case: When the numbers of times  x has to be added becomes 0. 
Recursive call: If the base case is not met, then add x to the current resultant value and pass it to the next iteration.
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program to Multiply two integers without// using multiplication, division and bitwise//  operators, and no loops#include<iostream> using namespace std;class GFG{     /* function to multiply two numbers x and y*/public : int multiply(int x, int y){    /* 0 multiplied with anything gives 0 */    if(y == 0)    return 0;     /* Add x one by one */    if(y > 0 )    return (x + multiply(x, y-1));     /* the case where y is negative */    if(y < 0 )    return -multiply(x, -y);}}; // Driver codeint main(){    GFG g;    cout << endl << g.multiply(5, -11);    getchar();    return 0;} // This code is contributed by SoM15242
#include<stdio.h>/* function to multiply two numbers x and y*/int multiply(int x, int y){   /* 0  multiplied with anything gives 0 */   if(y == 0)     return 0;    /* Add x one by one */   if(y > 0 )     return (x + multiply(x, y-1));    /* the case where y is negative */   if(y < 0 )     return -multiply(x, -y);} int main(){  printf("\n %d", multiply(5, -11));  getchar();  return 0;}
class GFG {         /* function to multiply two numbers x and y*/    static int multiply(int x, int y) {                 /* 0 multiplied with anything gives 0 */        if (y == 0)            return 0;             /* Add x one by one */        if (y > 0)            return (x + multiply(x, y - 1));             /* the case where y is negative */        if (y < 0)            return -multiply(x, -y);                     return -1;    }         // Driver code    public static void main(String[] args) {                 System.out.print("\n" + multiply(5, -11));    }} // This code is contributed by Anant Agarwal.
# Function to multiply two numbers# x and ydef multiply(x,y):     # 0 multiplied with anything    # gives 0    if(y == 0):        return 0     # Add x one by one    if(y > 0 ):        return (x + multiply(x, y - 1))     # The case where y is negative    if(y < 0 ):        return -multiply(x, -y)     # Driver codeprint(multiply(5, -11)) # This code is contributed by Anant Agarwal.
// Multiply two integers without// using multiplication, division// and bitwise operators, and no// loopsusing System; class GFG {         // function to multiply two numbers    // x and y    static int multiply(int x, int y) {                 // 0 multiplied with anything gives 0        if (y == 0)            return 0;             // Add x one by one        if (y > 0)            return (x + multiply(x, y - 1));             // the case where y is negative        if (y < 0)            return -multiply(x, -y);                     return -1;    }         // Driver code    public static void Main() {                 Console.WriteLine(multiply(5, -11));    }} // This code is contributed by vt_m.
<?php// function to multiply// two numbers x and yfunction multiply($x, $y){/* 0 multiplied withanything gives 0 */if($y == 0)    return 0; /* Add x one by one */if($y > 0 )    return ($x + multiply($x,                          $y - 1)); /* the case wherey is negative */if($y < 0 )    return -multiply($x, -$y);} // Driver Codeecho multiply(5, -11); // This code is contributed by mits.?>
<script> // javascript program to Multiply two integers without// using multiplication, division and bitwise//  operators, and no loops  /* function to multiply two numbers x and y*/function multiply( x,  y){    /* 0 multiplied with anything gives 0 */    if(y == 0)    return 0;     /* Add x one by one */    if(y > 0 )    return (x + multiply(x, y-1));     /* the case where y is negative */    if(y < 0 )    return -multiply(x, -y);}  // Driver code     document.write( multiply(5, -11)); // This code is contributed by todaysgaurav </script>
-55
Time Complexity: O(y) where y is the second argument to function multiply().
Auxiliary Space: O(y) for the recursion stack
Another approach: The problem can also be solved using basic math property
(a+b)2 = a2 + b2 + 2a*b
⇒  a*b = ((a+b)2 – a2 – b2) / 2
For computing the square of numbers, we can use the power function in C++ and for dividing by 2 in the above expression we can write a recursive function.
Below is the implementation of the above approach: 
C++
Python3
Javascript
// C++ program to Multiply two integers without// using multiplication, division and bitwise// operators, and no loops#include<bits/stdc++.h>using namespace std; // divide a number by 2 recursivelyint divideby2(int num){   if(num<2)    return 0;   return 1 + divideby2(num-2);} int multiply(int a,int b){    int whole_square=pow(a+b,2);    int a_square=pow(a,2);    int b_square=pow(b,2);         int val= whole_square- a_square - b_square;         int product;         // for positive value of variable val    if(val>=0)    product = divideby2(val);    // for negative value of variable val    // we first compute the division by 2 for    // positive val and by subtracting from    // 0 we can make it negative    else    product = 0 - divideby2(abs(val));         return product;} // Driver codeint main(){    int a=5;    int b=-11;    cout << multiply(a,b);    return 0;} // This code is contributed by Pushpesh raj.
# Python3 program to Multiply two integers without# using multiplication, division and bitwise# operators, and no loops # divide a number by 2 recursivelydef divideby2(num):     if(num < 2):        return 0    return 1 + divideby2(num-2) def multiply(a, b):    whole_square = (a + b) ** 2    a_square = pow(a, 2)    b_square = pow(b, 2)     val = whole_square - a_square - b_square     # for positive value of variable val    if(val >= 0):        product = divideby2(val)             # for negative value of variable val    # we first compute the division by 2 for    # positive val and by subtracting from    # 0 we can make it negative    else:        product = 0 - divideby2(abs(val))     return product # Driver codea = 5b = -11print(multiply(a, b)) # This code is contributed by phasing17
// JavaScript program to Multiply two integers without// using multiplication, division and bitwise// operators, and no loops // divide a number by 2 recursivelyfunction divideby2(num){   if(num<2)    return 0;   return 1 + divideby2(num-2);} function multiply(a, b){    let whole_square = Math.pow(a+b,2);    let a_square = Math.pow(a,2);    let b_square = Math.pow(b,2);     let val = whole_square- a_square - b_square;         let product;         // for positive value of variable val    if(val>=0)    product = divideby2(val);    // for negative value of variable val    // we first compute the division by 2 for    // positive val and by subtracting from    // 0 we can make it negative    else    product = 0 - divideby2(Math.abs(val));         return product;} // Driver codelet a = 5;let b = -11;console.log(multiply(a,b)); // This code is contributed by phasing17
-55
Russian Peasant (Multiply two numbers using bitwise operators)Please write comments if you find any of the above code/algorithm incorrect, or find better ways to solve the same problem. 
Mithun Kumar
SoumikMondal
todaysgaurav
subhammahato348
Code_r
pushpeshrajdx01
phasing17
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Sieve of Eratosthenes
Prime Numbers
Minimum number of jumps to reach end
Program to find GCD or HCF of two numbers
Find minimum number of coins that make a given value
The Knight's tour problem | Backtracking-1
Algorithm to solve Rubik's Cube
Program for Decimal to Binary Conversion
Modulo 10^9+7 (1000000007)