forked from vaibhavpathak999/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_multiplication.cpp
More file actions
55 lines (52 loc) · 1.35 KB
/
Copy pathstring_multiplication.cpp
File metadata and controls
55 lines (52 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <string>
#include <iostream>
using namespace std;
string strAdd(string s, string r)
{
int re = 0;
string digit;
if (r.length() < s.length())
r.insert(r.begin(), s.length() - r.length(), '0');
else if (r.length() > s.length())
s.insert(s.begin(), r.length() - s.length(), '0');
for (int i = s.length() - 1; i >= 0; --i)
{
int a = (int(s[i] + r[i]) + re - 96);
digit.insert(digit.begin(), char(a % 10 + 48));
re = a / 10;
}
if (re != 0)
digit.insert(digit.begin(), char(re + 48));
return digit;
}
string strMul(string s, string c)
{
string fina = "";
for (int j = c.length() - 1; j >= 0; j--)
{
string digit = "";
int re = 0;
for (int i = s.length() - 1; i >= 0; i--)
{
int a = int(c[j] - '0') * int(s[i] - '0') + re;
digit.insert(digit.begin(), char(a % 10 + 48));
re = a / 10;
}
if (re != 0)
digit.insert(digit.begin(), char(re + 48));
digit.append((c.length() - j - 1), '0');
fina = strAdd(fina, digit);
}
return fina;
}
int main()
{
string s;
string c;
cout << "Enter Number One" << endl;
cin >> s;
cout << "Enter Number Two" << endl;
cin >> c;
cout << "Multiplication Result: " << strMul(s, c) << endl;
return 0;
}