Program to multiply two strings and return result as string in C++


Suppose we have two numbers as string. We have to multiply them and return the result also in string. So if the numbers are “28” and “25”, then the result will be “700”

To solve this, we will follow these steps −

  • Taking two arguments x and y it indicates x divides y

  • if x < −Infinity and y = 1, then return infinity

  • a := |x|, b := |y| and ans := 0

  • while a − b >= 0

    • p := 0

    • while a − (left shifted b (left shifted 1 p times)) >= 0

      • p := p + 1

    • a := a − (left shift b, p times)

    • ans := ans + left shift 1 p times

  • if x > 0 is true and y > 0 is also true, then return ans, otherwise return (− ans)

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
   public:
   string multiply(string num1, string num2);
};
string Solution::multiply(string nums1, string nums2) {
   int n = nums1.size();
   int m = nums2.size();
   string ans(n + m, '0');
   for(int i = n - 1; i>=0; i−−){
      for(int j = m - 1; j >= 0; j−−){
         int p = (nums1[i] − '0') * (nums2[j] − '0') + (ans[i + j +
         1] − '0');
         ans[i+j+1] = p % 10 + '0';
         ans[i+j] += p / 10 ;
      }
   }
   for(int i = 0; i < m + n; i++){
      if(ans[i] !='0')return ans.substr(i);
   }
   return "0";
}
main(){
   Solution ob;
   cout << ob.multiply("28", "25");
}

Input

"28", "25"

Output

"700"

Updated on: 21-Oct-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements