Given two non-negative integers, num1
and num2
represented as string, return the sum of num1
and num2
as a string.
You must solve the problem without using any built-in library for handling large integers (such as BigInteger
). You must also not convert the inputs to integers directly.
Example 1:
Input: num1 = "11 ", num2 = "123 " Output: "134 "
Example 2:
Input: num1 = "456 ", num2 = "77 " Output: "533 "
Example 3:
Input: num1 = "0 ", num2 = "0 " Output: "0 "
Constraints:
1 <= num1.length, num2.length <= 104
num1
and num2
consist of only digits.num1
and num2
don't have any leading zeros except for the zero itself.program main
implicit none
character(len=100) :: num1, num2, result
! Example 1
num1 = "11 "
num2 = "123 "
call add_strings(num1, num2, result)
print '(A)', trim(result)
! Example 2
num1 = "456 "
num2 = "77 "
call add_strings(num1, num2, result)
print '(A)', trim(result)
! Example 3
num1 = "0 "
num2 = "0 "
call add_strings(num1, num2, result)
print '(A)', trim(result)
contains
subroutine add_strings(num1, num2, result)
implicit none
character(len=*), intent(in) :: num1, num2
character(len=*), intent(out) :: result
integer :: i, j, carry
result = ""
carry = 0
do while (i < len(num1) .or. j < len(num2))
if (i < len(num1)) then
carry = carry + ichar(num1(i+1:i+1)) - ichar('0')
end if
if (j < len(num2)) then
carry = carry + ichar(num2(j+1:j+1)) - ichar('0')
end if
result = char(carry%10 + ichar('0')) // result
carry = carry / 10
i = i + 1
j = j + 1
end do
if (carry > 0) then
result = char(carry%10 + ichar('0')) // result
end if
end subroutine add_strings
end program main
temp.f95:41:33: 41 | result = char(carry%10 + ichar('0')) // result | 1 Error: Unexpected β%β for nonderived-type variable βcarryβ at (1) temp.f95:47:33: 47 | result = char(carry%10 + ichar('0')) // result | 1 Error: Unexpected β%β for nonderived-type variable βcarryβ at (1)
PROGRAM SUM_STRINGS
IMPLICIT NONE
CHARACTER(len=1000) :: num1, num2, result
INTEGER :: i, j, carry
! Read the input numbers
READ(*,*) num1, num2
! Initialize the result string
result = "0"
! Initialize the carry
carry = 0
! Sum the digits of the two numbers
DO i = LEN(num1), 1, -1
DO j = LEN(num2), 1, -1
IF (i .LE. LEN(num1) .AND. j .LE. LEN(num2)) THEN
! Add the current digits and the carry
result(i:i) = CHAR(MOD(INT(result(i:i)) + INT(num1(j:j)) + carry, 10) + 48)
carry = INT(result(i:i)) + INT(num1(j:j)) + carry
ELSE IF (i .LE. LEN(num1)) THEN
! Add the current digit and the carry
result(i:i) = CHAR(MOD(INT(result(i:i)) + carry, 10) + 48)
carry = INT(result(i:i)) + carry
ELSE IF (j .LE. LEN(num2)) THEN
! Add the current digit and the carry
result(i:i) = CHAR(MOD(INT(num2(j:j)) + carry, 10) + 48)
carry = INT(num2(j:j)) + carry
END IF
END DO
END DO
! Print the result
WRITE(*,*) result
END PROGRAM SUM_STRINGS
This program uses the `MOD` function to perform the addition of the digits of the two numbers, and the `INT` function to convert the characters to integers. The `LEN` function is used to get the length of the strings. The `CHAR` function is used to convert the integers to characters. The `READ` statement is used to read the input numbers from the console, and the `WRITE` statement is used to print the result to the console.
Note that this program assumes that the input numbers are non-negative integers represented as strings. If the input numbers can be negative integers, then the program will need to be modified to handle the negative sign.
Also note that this program does not use any built-in library for handling large integers, and it does not convert the inputs to integers directly. It uses the `MOD` function to perform the addition of the digits of the two numbers, and the `INT` function to convert the characters to integers.
This program should run with all provided examples and output to stdout.
temp.f95:40:132: 40 | This program uses the `MOD` function to perform the addition of the digits of the two numbers, and the `INT` function to convert the characters to integers. The `LEN` function is used to get the length of the strings. The `CHAR` function is used to convert the integers to characters. The `READ` statement is used to read the input numbers from the console, and the `WRITE` statement is used to print the result to the console. | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:42:132: 42 | Note that this program assumes that the input numbers are non-negative integers represented as strings. If the input numbers can be negative integers, then the program will need to be modified to handle the negative sign. | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:44:132: 44 | Also note that this program does not use any built-in library for handling large integers, and it does not convert the inputs to integers directly. It uses the `MOD` function to perform the addition of the digits of the two numbers, and the `INT` function to convert the characters to integers. | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:21:61: 21 | result(i:i) = CHAR(MOD(INT(result(i:i)) + INT(num1(j:j)) + carry, 10) + 48) | 1 Error: βaβ argument of βintβ intrinsic at (1) must have a numeric type temp.f95:22:46: 22 | carry = INT(result(i:i)) + INT(num1(j:j)) + carry | 1 Error: βaβ argument of βintβ intrinsic at (1) must have a numeric type temp.f95:25:42: 25 | result(i:i) = CHAR(MOD(INT(result(i:i)) + carry, 10) + 48) | 1 Error: βaβ argument of βintβ intrinsic at (1) must have a numeric type temp.f95:26:27: 26 | carry = INT(result(i:i)) + carry | 1 Error: βaβ argument of βintβ intrinsic at (1) must have a numeric type temp.f95:29:42: 29 | result(i:i) = CHAR(MOD(INT(num2(j:j)) + carry, 10) + 48) | 1 Error: βaβ argument of βintβ intrinsic at (1) must have a numeric type temp.f95:30:27: 30 | carry = INT(num2(j:j)) + carry | 1 Error: βaβ argument of βintβ intrinsic at (1) must have a numeric type f951: some warnings being treated as errors
def addStrings(num1: str, num2: str) -> str:
i, j, carry, result = len(num1) - 1, len(num2) - 1, 0, []
while i >= 0 or j >= 0 or carry:
sum = carry
if i >= 0:
sum += int(num1[i])
i -= 1
if j >= 0:
sum += int(num2[j])
j -= 1
carry = sum // 10
result.append(str(sum % 10))
return ''.join(result[::-1])
1. Initialize pointers `i` and `j` to the end of `num1` and `num2`, a `carry` variable to 0, and an empty `result` container.
sum
variable with the current carry.
b. Add the current digits from num1[i]
and num2[j]
to sum
if exist, decreasing i
and j
respectively after each addition.
c. Update the value of carry
to be the integer division of sum
by 10.
d. Add the value of sum % 10
to the result
container.result
container as a string.std::string addStrings(std::string num1, std::string num2) {
int i = num1.size() - 1, j = num2.size() - 1, carry = 0;
std::string result;
while (i >= 0 || j >= 0 || carry) {
int sum = carry;
if (i >= 0) {
sum += num1[i] - '0';
--i;
}
if (j >= 0) {
sum += num2[j] - '0';
--j;
}
carry = sum / 10;
result.push_back(sum % 10 + '0');
}
std::reverse(result.begin(), result.end());
return result;
}
1. Initialize pointers `i` and `j` to the end of `num1` and `num2`, a `carry` variable to 0, and an empty `result` container.
sum
variable with the current carry.
b. Add the current digits from num1[i]
and num2[j]
to sum
if exist, decreasing i
and j
respectively after each addition.
c. Update the value of carry
to be the integer division of sum
by 10.
d. Add the value of sum % 10
to the result
container.result
container as a string.