Modified Decimal Addition Algorithm (Different
Lengths)
Given: m ≥ 1, n ≥ 1 where m may not be equal to n, and two positive numbers:
a_{m-1}a_{m-2}...a_0 and b_{n-1}b_{n-2}...b_0.
Wanted: c_k c_{k-1} ... c_0, where k = max(m, n).
Algorithm:
Step 1: Set the value of carry to 0
Step 2: Set the value of i to 0
Step 3: While the value of i is less than or equal to k - 1 repeat Steps 4 through 6
Step 4: If i ≤ m - 1, let a_i = digit of a; otherwise set a_i = 0.
If i ≤ n - 1, let b_i = digit of b; otherwise set b_i = 0.
Add a_i + b_i + carry to get c.
Step 5: If c ≥ 10, then set c_i = c - 10 and carry = 1; otherwise, set c_i = c and carry = 0.
Step 6: Add 1 to i (move to next column).
Step 7: Set c_k = carry.
Step 8: If c_k = 1, then print c_k, c_{k-1}, ..., c_0; otherwise, print c_{k-1}, ..., c_0.
Step 9: Stop.