Skip to content
Related Articles

Related Articles

Multiply two integers without using multiplication, division and bitwise operators, and no loops
  • Difficulty Level : Easy
  • Last Updated : 15 Mar, 2021

By making use of recursion, we can multiply two integers with the given constraints. 
To multiply x and y, recursively add x y times. 
 

C++




// 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 code
int main()
{
    GFG g;
    cout << endl << g.multiply(5, -11);
    getchar();
    return 0;
}
 
// This code is contributed by SoM15242

C




#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;
}

Java




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.

Python3




# Function to multiply two numbers
# x and y
def 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
print(multiply(5, -11))
 
# This code is contributed by Anant Agarwal.

C#




// Multiply two integers without
// using multiplication, division
// and bitwise operators, and no
// loops
using 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




<?php
// 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
echo multiply(5, -11);
 
// This code is contributed by mits.
?>

Javascript




<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>

Output: 

-55

Time Complexity: O(y) where y is the second argument to function multiply().
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.
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :