Multiplication Without Using * Operator
Explore how to implement multiplication of two integers using recursion instead of the multiply operator. Understand the base cases for zero and one, and how recursive calls break down the problem. This lesson helps build a deeper understanding of recursion and problem-solving techniques useful in coding interviews.
We'll cover the following...
We'll cover the following...
Problem statement
In this problem, we need to find the result of the multiplication of two numbers, a and b but without using * operator. This looks like a pretty simple problem. We can easily iterate a loop from 1 to b and add a to our result. But there is a catch: you need to write a solution with a Recursive approach.
Implementation
Let’s write the solution first, and then visualize how recursion is working.
Explanation:
- On line 4, we define our recursive function
prod(). - On line 5, we check for the base case as