Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

Example 1:

Input: num1 = "2", num2 = "3"
Output: "6"
Example 2:

Input: num1 = "123", num2 = "456"
Output: "56088"

Note:
1. The length of both num1 and num2 is < 110.
2. Both num1 and num2 contain only digits 0-9.
3. Both num1 and num2 do not contain any leading zero, except the number 0 itself.
4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

First we shall look how we do normal multiplication:

If we try to multiply integer num_1 = “1 2 3” with num_2 = “4 5 6”, below are the steps we follow:

Step 1: Starting from the right hand side of num_2, take a single digit and multiply with all the digits of num_1.

Step 2: When we go to the second digit of num_2, we leave a single space on the right side of the result. Similarly, we do it for the 3rd digit, we leave 2 space on the right side.

Step 3: Then we add all the numbers to get the final result.

Note: While multiplying, there is a possibility of number getting a carry, then we need to carry it to the next digit.

The above 3 steps can be visualized as shown below:

Multiply by 6 first:

Multiply by 5:

Multiply by 4:

Sum the total result:

Now we have covered the basic, how do we achieve it programmatically?
Consider the below image, we are trying to get the result of “1 2 3 [in green]” * “4 5 [in blue]”.

From the image below, we can conclude that
`num_1[i] * num_2[j]` will be placed at indices `[i + j`, `i + j + 1]`

Hence we can write it up programmatically as shown below:

/*
* File     : multiply_strings.cpp
* Author   : ajay.thousand@gmail.com
* Purpose  : to multiply strings and then display the result in strings
* Copyright: @ prodevelopertutorial.com
*/

#include<iostream>
using namespace std;

string multiply_strings(string num_1, string num_2) 
{
    int len_1 = num_1.length(); 
    int len_2 = num_2.length();

    //create space to store the result and fill it with zero's
    string final_result(len_1 + len_2, '0');
    
    //reverse both number 1 and number 2, bacause we calcuate it from reverse
    reverse(num_1.begin(), num_1.end());
    reverse(num_2.begin(), num_2.end());

    //we use 2 loops because, we have to multiply 2 numbers 
    for (int i = 0; i < len_1; i++)
    {
        for (int j = 0; j < len_2; j++) 
        {
            
            int tmp_result = (final_result[i + j] - '0') + (num_1[i] - '0') * (num_2[j] - '0'); // we use num_1[i] - '0' as it will give the integer value of that number
                
            final_result[i + j] = '0' + tmp_result % 10;

            final_result[i + j + 1] += tmp_result / 10;
        }
    }
    
    for (int i = len_1 + len_2 - 1; i > 0 && final_result[i] == '0'; i--)
        final_result.pop_back();

    reverse(final_result.begin(), final_result.end());
    return final_result;
}


int main() 
{
    string num_1 = "123";
    string num_2 = "456";

    string result = multiply_strings(num_1, num_2);
    cout << "The result of "<<num_1 <<" * "<<num_2 <<" is = "<<result<<endl;
    return 0;
}

Output:

The result of 123 * 456 is = 56088

This solution can also be solved using grid multiplication. If anyone can write the solution for that, it will be useful. I shall provide the solution for that soon.

Write a Comment

Leave a Comment

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