
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Program to add two binary strings, and return also as binary string in C++
- MySQL query to return a string as a result of IF statement?
- Multiply Large Numbers represented as Strings in C++
- MySQL query to multiply values of two rows and add the result
- Program to add two numbers represented as strings in Python
- Function to check two strings and return common words in JavaScript
- Program to multiply two matrices in C++
- C# program to multiply two matrices
- C++ Program to Multiply two Numbers
- Java program to multiply two matrices.
- Python program to multiply two matrices
- Program to create a lexically minimal string from two strings in python
- Multiply Strings in C++
- How to multiply two vectors in R as in mathematics?
- Python Program to Take in Two Strings and Display the Larger String without Using Built-in Functions

Advertisements