Answer
Decimal addition algorithm with m and n digit input values
Algorithm:
Algorithm is the group of simple instruction to accomplish a task.
• Algorithm is the description of the procedure to solve the problem.
• It provides the step by step explanation of the solution to the problem.
• It plays the main role in mathematical problems because it provides a finite number of steps that contain a repetition operation.
Work Step by Step
Algorithm for adding the m -digit and n -digit numbers:
Assume that the given input values are:
• The two number of digits are m and n.
• The value “a” contains the m-digit positive numbers and the value “b” contains the n-digit positive numbers .
• Here, the two number of digits do not have the same number of digits. That is, m may or may not be equal to n.
• If the m-digit is not equal to n-digit, then,
o Add the non-significant leading zeros to shorter number of digits by resetting the two number of digits to the same number of digits.
Modified decimal addition algorithm:
Step 1: If (n > m), then
Set i to 0.
While ( m+i < n)
Adding a leading zero to the number at position .
Increment i by 1.
End of the loop.
Else
If (m > n), then
Set i to 0.
While ( n+i < m)
Adding a leading zero to the number at position
.
Increment i by 1.
End of the loop.
• Now, the two numbers of digits are equal in length. Then, set the larger value of two digits to “m” variable:
o Step 2: Set m to the larger value of m and n digits.
• The Sum of m and n digits are .
Where,
o Step 3: Set the value of carry to 0.
o Step 4: Set the value of i to 0.
o Step 5: While the value of i is less than or equal to m-1, repeat the
instructions in Step 6 through Step 8.
o Step 6: Add the two digits and to the current value of carry
to get .
o Step 7: If , then reset the to () and reset the
value of carry to 1;
Otherwise, set the new value of carry to 0.
o Step 8: Add 1 to i, effectively moving one column to the
left.
o Step 9: Set to the value of carry.
o Step 10: Print the final answer
o Step 11: Stop
• If both the digits m and n are equal, then,
o The above modified algorithm does not work. So, it proceeds with the algorithm of decimal addition.
Decimal addition algorithm:
o Step 1: Set the value of carry to 0.
o Step 2: Set the value of i to 0.
o Step 3: While the value of i is less than or equal to m-1, repeat the instructions in Step 4 through Step 6.
o Step 4: Add the two digits and to the current value of carry to get .
o Step 5: If , then reset the to () and reset the
value of carry to 1;
Otherwise, set the new value of carry to 0.
o Step 6: Add 1 to i, effectively moving one column to the left.
o Step 7: Set to the value of carry.
o Step 8: Print the final answer .
o Step 9: Stop
Example for m and n digits are not equal:
The sample input values are:
• The number of digits m = 3 and n = 2.
• The m-digits positive numbers are and the n-digits positive numbers are .
• Here, the two number of digits are not have the same number of digits. That is, m is not equal to n ( 3 is not equal to 2). So, it proceed with the modified decimal addition algorithm.
o Add the non-significant leading zeros to shorter number of digits by resetting the two number of digits to the same number of digits.
o Step 1: If (2 > 3), the condition fails. Then
Go to else part.
Else
If (3 > 2), then
Set i to 0.
While ( 2+0 < 3)
Adding the 0 to . That is,
Increment i to (0 + 1) = 1.
End of the loop.
• Now, the two number of digits m and n are equal in length. Then, set the larger value of two digits to “m” variable:
o Step 2: Set m to 3.
• The Sum of m and n digits are
Where,
o Step 3: Set the value of carry = 0.
o Step 4: Set the value of i = 0.
o Step 5: While the value of (), repeat the instructions in Steps 6
through Step 8.
First iteration loop: Here, i = 0, then check whether the condition is, repeat the
instructions in Step 6 through Step 8.
o Step 6(I): Add the two digits and to the current value of carry = 0. Then, () which is (9+9+0) to produce the result as .
o Step 7(I): If (), the condition is satisfied, then
Subtract the value of (18 – 10) to get 8 as and then reset the value of and reset the value of carry to 1.
o Step 8(I): Add i to (0+1) = 1.
Second iteration loop: Here, i = 1, again check whether the condition is (), repeat the
instructions in Step 6 through Step 8.
o Step 6(II): Add the two digits and to the current value of carry = 1. Then, which is (4+2+1) to produce the result as .
o Step 7(II): If (), the condition fails, then
Set the new value of carry to 0.
o Step 8(II): Add i to (1+1) = 2.
Third iteration loop: Here, i = 2, again check whether the condition is (), repeat the
instructions in Step 6 through Step 8.
o Step 6(III): Add the two digits and to the current value of carry = 0. Then, which is (1+0+0) to produce the result as .
o Step 7(III): If (), the condition fails, then
Set the new value of carry to 0.
o Step 8(III): Add i to (2+1) = 3.
Fourth iteration loop: Here, i = 3, again check whether the condition is (), the condition fails. So, it does not repeat the instructions in Steps 4 through 6. But, instead go to Step 9.
o Step 9: Set to the value of carry, then .
o Step 10: Print the final answer .
o Step 11: Stop the process.
Therefore, the m-digit number is 149 and n-digit number is 29 and adding the m and n digit numbers produces .