-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigNumSum.cpp
More file actions
43 lines (32 loc) · 784 Bytes
/
Copy pathBigNumSum.cpp
File metadata and controls
43 lines (32 loc) · 784 Bytes
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
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
string work(string s1, string s2)
{
int size1 = s1.size() - 1;
int size2 = s2.size() - 1;
int carry = 0;
string sum = "";
while(size1>=0 || size2>=0)
{
int digit1 = (size1>=0) ? s1[size1]- '0' : 0;
int digit2 = (size2>=0) ? s2[size2] - '0' : 0;
int d = digit1 + digit2 + carry;
carry = d/10;
d %= 10;
sum += ('0' + d);
size1--, size2--;
}
if(carry) sum += ('0' + carry);
while(sum.size()>1 && sum.back()=='0') sum.pop_back();
reverse(sum.begin(), sum.end());
return sum;
}
//Driver code
int main()
{
string str1, str2;
cin >> str1 >> str2;
cout << work(str1, str2) << endl;
return 0;
}