Huge Addition

Matlab. This was an interesting assignment in the Matlab course I took before. Its inputs and output could be very large, in fact, larger than representable number in Matlab. Solution can show its output with full digits even if it is more than 16 digits.

For example;

input 1 = ‘392819655968612702353430868739406976733152178677243’

input 2 = ‘800648666501228076207488540123’

Question: Write function called huge_add that adds together two positive integers of any length specified as char vectors using decimal notation. The single output argument is the result and it is a char vector as well. The inputs and output must contain digits only; no commas, spaces or any other characters are allowed. If any of these assumptions are violated by the input, the function returns the number -1. Hint: you cannot use any of the built-in numerical data types of MATLAB to do the calculation since the function needs to work for numbers that are larger than what can be represented as a number in MATLAB.

Solution: After checking if the inputs are char or not, char inputs were converted to double and passed to the calculation stage. I wrote an function inside called “calculator” for addition creating a char vector by taking care of any carries. In the final stage, this function is called after the input sizes are compared and zeros are added to close the digit gap between the inputs.

answer = ‘392819655968612702354231517405908204809359667217366’