In: Mechanical Engineering
3. a) Write a script to prompt the user for the length and width of a rectangle, and print its area with two decimal places. Put comments in the script. b) Convert the script in part (a) to a function.
a)Script for area of the rectanlge
clear, clc % clear the previous variables and clear screen
l = input('Enter the length of the rectangle') % Prompt for length
of the rectangle
b = input('Enter the width of the rectangle') %Prompt for width of
the rectangle
area = l*b; % formula for area of the rectangle
fprintf('The Area of the rectangle is = %.2f\n',area); % Print the
area with two decimal
b) Convert script to function
%save this file as arearec.m when call the function with pre defined variables
%example
%arearec(5,3) then press enter
%or
%if you already assign the values for l an b then you call the
function as arearec(l,b)
function arearec(l,b) %define function name as arearec
area = l*b;
fprintf('The Area of the rectangle is = %.2f\n',area);
end