Untitled
unknown
plain_text
2 years ago
680 B
8
Indexable
#include <iostream>
using namespace std;
string findSum(string str1, string str2)
{
string str = ""
int n1 = str1.length(), int n2 = str2.length();
reverse(str1.begin(), str1.end());
reverse(str2.begin(), str2.end());
int carry = 0;
for (int i=0; i<n1; i++)
{
int sum = ((str1[i])+(str2[i])+carry);
str.push_back(sum%10 );
carry = sum/10;
}
for (int i=n1; i<n2; i++)
{
int sum = ((str2[i])+carry);
str.push_back(sum%10 );
carry = sum/10;
}
if (carry)
str.push_back(carry);
reverse(str.begin(), str.end());
return str;
}
// Driver code
int main()
{
string str1 = "12";
string str2 = "198111";
cout << findSum(str1, str2);
return 0;
}
Editor is loading...
Leave a Comment