Multiply Strings in C++

In this program, we are going to multiply two strings in c++. We will not use any built-in function to convert a string to an integer.

In this program, we are going to multiply two strings in c++. We will not use any built-in function to convert a string to an integer.

Pre-requisite:
  • Loops
  • Add Strings
Output:

Input: num1 = "80", num2 = "2"
Output: "160"

Multiply Strings in C++

In this program, we have performed simple multiplication of two strings. We picked up the last character of the second number and multiplied it with each character of the first number and pushed that multiplication result in a vector of sum. Then we performed the addition of all the strings in the vector of sum.

#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    string addStrings(string &num1, string &num2)
    {
        int n = num1.size() - 1;
        int m = num2.size() - 1;
        string s;
        int carry = 0;
        int x1, x2, value;
        while (n >= 0 || m >= 0)
        {
            if (n >= 0)
            {
                x1 = num1[n] - '0';
            }
            else
                x1 = 0;
            if (m >= 0)
            {
                x2 = num2[m] - '0';
            }
            else
                x2 = 0;

            value = (x1 + x2 + carry) % 10;
            carry = (x1 + x2 + carry) / 10;
            s.push_back(value + '0');
            n--;
            m--;
        }
        if (carry != 0)
            s.push_back(carry + '0');
        reverse(s.begin(), s.end());
        return s;
    }
    string multiply(string num1, string num2)
    {
        vector<string> sum;

        int m = num2.size() - 1;
        int last = 0;
        while (m >= 0)
        {
            string s;
            int carry = 0;
            int x1, x2, value;
            int n = num1.size() - 1;
            while (n >= 0)
            {
                x1=num1[n]-'0';
                x2=num2[m]-'0';

                value = (x1 * x2 + carry) % 10;
                carry = (x1 * x2 + carry) / 10;
                s.push_back(value + '0');
                n--;
            }
            if (carry != 0)
                s.push_back(carry + '0');

            reverse(s.begin(), s.end());
            int temp = last;
            while (temp--)
                s.push_back('0');
            last++;
            sum.push_back(s);
            m--;
        }
        string ans = "0";
        for (auto str : sum)
        {
            ans = addStrings(ans, str);
        }

        if (ans[0] == '0')
        {
            int i = 1;
            while (i < ans.size() && ans[i] == '0')
            {
                ans.erase(ans.begin() + i);
                // i++;
            }
        }

        return ans;
    }
};
int main()
{
    Solution s;
    string str1,str2;
    cout<<"Enter First String:";
    cin>>str1;
    cout<<"Enter Second String:";
    cin>>str2;
    cout<<s.multiply(str1,str2)<<endl;
}

Multiply Strings | Leetcode 43 Solution


In my childhood, my grandfather used to give me multiplication of numbers that have more than 10 digits so that I can't cheat. He knows a trick to verify the answer of multiplication without actually multiplying them. So, I had to multiply them without any cheating. Today, I made a program that can multiply any two numbers, regardless of digits.

Post a Comment

Please do not enter any spam link in the comment box.