-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-strings.cpp
39 lines (39 loc) · 919 Bytes
/
add-strings.cpp
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
// Implemented by Kritagya Kumra
#include <iostream>
using namespace std;
string addStrings(string num1, string num2)
{
int i = num1.length() - 1;
int j = num2.length() - 1;
string answer = "";
int carry = 0;
char value;
int sum = 0;
while (i >= 0 && j >= 0)
{
sum += carry;
if (i > 0)
sum = sum + num1[i] - 48;
if (j > 0)
sum = sum + num2[j] - 48;
value = (sum % 10) + 48;
answer += value;
carry = sum / 10;
i--;
j--;
}
value = carry + 48;
if (carry)
answer = answer + value;
reverse(answer.begin(), answer.end());
}
int main()
{
string num1, num2;
cout << "Enter your first number " << endl;
cin >> num1;
cout << "Enter your second number " << endl;
cin >> num2;
cout << "The answer is " << addStrings(num1, num2);
}
// Implemented by Kritagya Kumra