MATLAB program Write a script to add 30digit numbers and pri
MATLAB program
Write a script to add 30-digit numbers and print the result. This is not as easy as it might sound at first, because integer types are may not be able to store a value as large as this one. One way to handle large integers is to store them in vectors, where each element in the vector store a digit of the integer. Create original numbers using the randi function. MATLAB programSolution
function z = addTwoNums(x, y)
z =[];
carry = 0;
for i=30:-1:1
sum = x(i) + y(i) + carry;
if sum > 9
carry = 1;
sum = rem(sum,10);
end
z(i) = sum;
end
z = [carry z];
end
% Prints the number equivalent of the digits in an array
function printNum(x, digits)
for i = 1:digits
fprintf(\'%d\',x(i));
end
fprintf(\'\ \');
end
% Generate two random 30 digit numbers
x = randi([0,9],1,30);
y = randi([0,9],1,30);
fprintf(\'Number-1 : \');
printNum(x, 30);
fprintf(\'Number-2 : \');
printNum(y, 30);
% Compute the sum
z = addTwoNums(x,y);
fprintf(\'Addition : \');
printNum(z, 31);
