Your task is to write a program that reads pairs of natural numbers and prints their sum.
Warning: the natural numbers that we consider in this problem can be very large (thousands of digits), you must store them in variables of |string| type. The main program is already done; do not modify it:
int main() {
string x, y;
while (cin >> x >> y) cout << sum(x, y) << endl;
}
Implement the function
string sum(string x, string y);
that, given two strings of characters |x| and |y| that represent two natural numbers, returns a string of characters |z| that represents their sum.
To simplify the problem, suppose that neither |x| nor |y| start with ‘|0|’. Besides, suppose that |x| has, at least, the same number of digits than |y|. The number of digits of |z| must be equal to the number of digits of |z| plus one, although it causes that |z| starts with ‘|0|’.
Remember that a string |s| with |n| characters |c| can be declared like this: |string s(n, c);|
Remember also that the string operations like |s += ’0’;|, |s1 += s2;| or |s = s1 + s2;| are not allowed.
Author: Unknown
Translator: Carlos Molina
Generation: 2026-01-25T11:20:38.693Z
© Jutge.org, 2006–2026.
https://jutge.org