

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#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"
- Related Questions & Answers
- Program to add two binary strings, and return also as binary string in C++
- Multiply Large Numbers represented as Strings in C++
- MySQL query to multiply values of two rows and add the result
- MySQL query to return a string as a result of IF statement?
- Program to add two numbers represented as strings in Python
- C++ Program to Multiply two Numbers
- C# program to multiply two matrices
- Java program to multiply two matrices.
- Python program to multiply two matrices
- How to multiply two vectors in R as in mathematics?
- Program to multiply two matrices in C++
- Multiply Strings in C++
- Function to check two strings and return common words in JavaScript
- 8051 Program to Multiply two 8 Bit numbers
- 8085 Program to Multiply two 8 bits numbers
Advertisements