Background A simple way to perform addition is to use the method taught to child
ID: 3838551 • Letter: B
Question
Background A simple way to perform addition is to use the method taught to children. You align the columns of each number (starting with theone's place) and add the individual digits within each column, being sure to account for the "carry", if any, from a previous summation. Example: What is the value of 35 28? (Answer: 63) 10000 s 1000 s 100's 10's Place place place place place Carry Sum J N1+N2 Example: What is the value of 1500 21? (Answer: 1521) 10000 s 1000's 100's 10's Place place place place place Carry Sum N1+N2 Example: What is the value of 9908 4252? (Answer: 14160) 10000 s 1000 s 100's 10's Place place place place place CarryExplanation / Answer
As the programming language isn't specified, I am going to use this liberty and explain the logic behind this. Let's get started:
Take String as input, can be taken in any language and verify is't no more than 100 characters. It's very simple, use a do while loop with the condition length of the string is greater than 100. Loop won't exit until, length requirement is met.
do{ take input } while(string.length()>100)
Extracting the digits into an integer array --> Run a loop for the length of the string and store. Now, how to store? I would recommend you store the way input was given. It will make our calculations easier. Same goes for second number also.
Addition part, remember to initialize one more array with 101 elements(including the overflow). Now we will add column-wise using the function add_array(array1, array2, destination). They have to be called by address, so our job is made easier.
{ c=0 --> carry initiated
smaller_array(array1,array2).length --> this many times we have run the summation
for(i=0;i<lengthof(smaller_array);i++){
dest[i]=array1[i] + array2[i] + c; --> adding column wise
c=dest[i] / 10; --> checking for carry
dest[i] = dest[i] % 10; --> no value can be greater than 10
put a print for the sum(dest[i]), carry and two numbers
}
for(;i<lengthof(larger_array);i++){
dest[i] = --> copying the rest of the numbers as it is. Add carry and do check for sum < 10
}
}
Call for print_array(array,length) to print all the three arrays. Use loop to print it.
Now, we want the whole program to run unless user wants to quit. So, put the complete thing into a while loop.
while(true){
Ask for: Enter another set(Y/N): take input in var
if(var==N) break; --> come out of the loop
}
Our conditions of writing at leat two function is also met. I hope this was helpful and if you have any further queries, do let me know !
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.