For any given two numbers n and m, you have to find n*m without using any multiplication operator.

**Examples :**

Input: n = 25 , m = 13 Output: 325 Input: n = 50 , m = 16 Output: 800

We can solve this problem with the shift operator. The idea is based on the fact that every number can be represented in binary form. And multiplication with a number is equivalent to multiplication with powers of 2. Powers of 2 can be obtained using left shift operator.

Check for every set bit in the binary representation of m and for every set bit left shift n, count times where count if place value of the set bit of m and add that value to answer.

## C++

`// CPP program to find multiplication ` `// of two number without use of ` `// multiplication operator ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function for multiplication ` `int` `multiply(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `ans = 0, count = 0; ` ` ` `while` `(m) ` ` ` `{ ` ` ` `// check for set bit and left ` ` ` `// shift n, count times ` ` ` `if` `(m % 2 == 1) ` ` ` `ans += n << count; ` ` ` ` ` `// increment of place value (count) ` ` ` `count++; ` ` ` `m /= 2; ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 20 , m = 13; ` ` ` `cout << multiply(n, m); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find multiplication ` `// of two number without use of ` `// multiplication operator ` `class` `GFG ` `{ ` ` ` ` ` `// Function for multiplication ` ` ` `static` `int` `multiply(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` `int` `ans = ` `0` `, count = ` `0` `; ` ` ` `while` `(m > ` `0` `) ` ` ` `{ ` ` ` `// check for set bit and left ` ` ` `// shift n, count times ` ` ` `if` `(m % ` `2` `== ` `1` `) ` ` ` `ans += n << count; ` ` ` ` ` `// increment of place ` ` ` `// value (count) ` ` ` `count++; ` ` ` `m /= ` `2` `; ` ` ` `} ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `20` `, m = ` `13` `; ` ` ` ` ` `System.out.print( multiply(n, m) ); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# python 3 program to find multiplication ` `# of two number without use of ` `# multiplication operator ` ` ` `# Function for multiplication ` `def` `multiply(n, m): ` ` ` `ans ` `=` `0` ` ` `count ` `=` `0` ` ` `while` `(m): ` ` ` `# check for set bit and left ` ` ` `# shift n, count times ` ` ` `if` `(m ` `%` `2` `=` `=` `1` `): ` ` ` `ans ` `+` `=` `n << count ` ` ` ` ` `# increment of place value (count) ` ` ` `count ` `+` `=` `1` ` ` `m ` `=` `int` `(m` `/` `2` `) ` ` ` ` ` `return` `ans ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `20` ` ` `m ` `=` `13` ` ` `print` `(multiply(n, m)) ` ` ` `# This code is contributed by ` `# Ssanjit_Prasad ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find multiplication ` `// of two number without use of ` `// multiplication operator ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function for multiplication ` ` ` `static` `int` `multiply(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` `int` `ans = 0, count = 0; ` ` ` `while` `(m > 0) ` ` ` `{ ` ` ` `// check for set bit and left ` ` ` `// shift n, count times ` ` ` `if` `(m % 2 == 1) ` ` ` `ans += n << count; ` ` ` ` ` `// increment of place ` ` ` `// value (count) ` ` ` `count++; ` ` ` `m /= 2; ` ` ` `} ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 20, m = 13; ` ` ` ` ` `Console.WriteLine( multiply(n, m) ); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find multiplication ` `// of two number without use of ` `// multiplication operator ` ` ` `// Function for multiplication ` `function` `multiply( ` `$n` `, ` `$m` `) ` `{ ` ` ` `$ans` `= 0; ` `$count` `= 0; ` ` ` `while` `(` `$m` `) ` ` ` `{ ` ` ` `// check for set bit and left ` ` ` `// shift n, count times ` ` ` `if` `(` `$m` `% 2 == 1) ` ` ` `$ans` `+= ` `$n` `<< ` `$count` `; ` ` ` ` ` `// increment of place value (count) ` ` ` `$count` `++; ` ` ` `$m` `/= 2; ` ` ` `} ` ` ` `return` `$ans` `; ` `} ` ` ` `// Driver code ` `$n` `= 20 ; ` `$m` `= 13; ` `echo` `multiply(` `$n` `, ` `$m` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

260

**Time Complexity :** O(log n)

**Related Article:**

Russian Peasant (Multiply two numbers using bitwise operators)

This article is contributed by **Shivam Pradhan (anuj_charm)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Multiply a number with 10 without using multiplication operator
- Divide two integers without using multiplication, division and mod operator
- Multiplying a variable with a constant without using multiplication operator
- Left Shift and Right Shift Operators in C/C++
- Bitwise right shift operators in Java
- Multiplication with a power of 2
- Booth’s Multiplication Algorithm
- Calculate 7n/8 without using division and multiplication operators
- Check if left and right shift of any string results into given string
- Find XOR of two number without using XOR operator
- Compare two integers without using any Comparison operator
- Check a number is odd or even without modulus operator
- Multiply any Number with 4 using Bitwise Operator
- Maximum OR value of a pair in an Array without using OR operator
- Find the remainder when N is divided by 4 using Bitwise AND operator
- Find largest element from array without using conditional operator
- Sum of numbers with exactly 2 bits set
- Sum of bit differences for numbers from 0 to N | Set 2
- Find two numbers from their sum and XOR
- Equal Sum and XOR of three Numbers