d Consider the case where the input to a program can be eith
Solution
MATLAB CODE :
clc;
clear all;
str=input(\'Type of Operation(add,sub,mul,div) :\',\'s\'); % takes input operation as string type
x=input(\'Enter first variable : \'); % variables in scalar , vector and matrix
y=input(\'Enter second variable : \');
r=0; %intializing result
add_num= strcmp(\'add\',str); % if string comparision true it returns \'1\' else \'0\'
sub_num = strcmp(\'sub\',str);
mul_num= strcmp(\'mul\',str);
div_num= strcmp(\'div\',str);
if add_num==1 %compares wheather add is typed
r=x+y; %addition
fprintf(\'Result = %4.2f\',r); %prints results in 2 decimal places
else if sub_num==1
r=x-y;
fprintf(\'Result = %4.2f\',r);
else if mul_num ==1
[rowy, coly] = size(y); % determines rows and columns
[rowx, colx] = size(x);
if colx==rowy % checks condition for matrix multplication
r=x*y;
else fprintf(\'Error--- columns of 1st match rows of 2nd\');
end
fprintf([repmat(\'%4.2f\\t\', 1, size(r, 2)) \'\ \'], r\'); % prints result as matrix
else if div_num==1
[rowy, coly] = size(y);
[rowx, colx] = size(x);
if colx==rowy
else if rowy==coly
r=x/y;
else fprintf(\'Error ---division requires square matrix\');
end
end
fprintf([repmat(\'%4.2f\\t\', 1, size(r, 2)) \'\ \'], r\');
end
end
end
end

