Multiply Integers without Multiplication Arithmetic Operator in C++

Confronting with numbers is the first thing each one of us has learnt in schools. However, as we step up high more and more difficult questions based on the same fundamentals seem to appear in our way. It’s the same with programming, using the same fundamentals to find solutions for multiple complex problems. Now the cool thing is, you can be creative in your way eventually coming up with a unique solution. And if you’re able to do this, then you’ve clearly understood how to use programming for others and your good. But keeping that aside, we’ve talked about numbers before. So what if we do simple multiplication, seems too easy right!. Well, it doesn’t cause we’re not going to use the multiplication Arithmetic Operator when writing this program. So let’s quickly find out how to Multiply Integers without Multiplication Arithmetic Operator in C++.

With the help of this program, you will be able to find the multiplication of two integers without using the Multiplication Arithmetic Operator. Practising these types of questions also helps to get an upper edge in Competitive Programming.

 

What’s The Approach?

 

  • We all know multiplication of any two numbers is just the addition of them multiple times. Therefore we’ll use recursion to solve the problem under the given precondition.

 

  • Consider Two Integers X & Y we want to multiply with each other. Therefore we will Add X With Itself Y Times.

 

  • So we will simply create a function to perform the above instructions recursively.

 

Also Read: Create A Stack Using Array in C++

 

C++ Program To Multiply Integers Without Multiplication Arithmetic Operator

 

Input: 

 

5, -11

 

Output:

 

-55

 

// C++ program to Multiply two integers without
// using multiplication, operator
#include<iostream>

using namespace std;
class TechDecodeTutorials
{
    
/* function to multiply two numbers x and y*/
public : int multiply(int x, int y)
{
    /* 0 multiplied anything gives 0 */
    if(y == 0)
    return 0;

    /* Add x one by one */
    if(y > 0 )
    return (x + multiply(x, y-1));

    /* case where y is negative */
    if(y < 0 )
    return -multiply(x, -y);
}
};

// Driver code
int main()
{
     TechDecodeTutorials TDT;
    cout << endl << TDT.multiply(5, -11);
    getchar();
    return 0;
}

 

Ethix

I'm a coding geek interested in cyberspace who loves to write and read

Leave a Reply

Your email address will not be published. Required fields are marked *