Write a MATLAB program for Unit Converter using Switch
Code:
clear;
clc;
disp('Unit Converter (cm to inch,meter,millimeter,kilometer)');
x=input('Enter the Unit value \n');
units=input('Enter the Unit Type(in/inch,m/meter,mm/millimeter,km/kilometer)
\n','s');
switch units
case {'in','inch'}
y = 2.54*x;
disp ([num2str(x)
' '
units ' converted to cm is :' num2str(y)]);
case {'m','meter'}
y = x*100;
disp ([num2str(x)
' '
units ' converted to cm is :' num2str(y)]);
case { 'millimeter','mm'}
y = x/10;
disp ([num2str(x)
' '
units ' converted to cm is :' num2str(y)]);
case {'km','kilometer'}
y = x*100000;
disp ([num2str(x)
' '
units ' converted to cm is :' num2str(y)]);
otherwise
disp (['unknown
units:' units])
y = nan; %
not a number
end
Leave a Comment