In: Computer Science
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab function error).
function letter = grade (percent)
% Inputs: percent -- percentage grade
%
% Output: letter -- letter grade
%
% For example,
% l = grade (85) should return B
% l = grade (90) should return A
% l = grade (35) should return Z
Need to submit Matlab function online to check if if successful. Please help!
SOURCE CODE:
percent=0
function letter=grade(percent) %function grade() for calculating
grade
if percent>=90 %if percent is greater than equal to 90
letter='A'
return
%if percent is greater than equal to 80 and less than 90
elseif percent>=80 && percent<90
letter='B'
return
%if percent is greater than equal to 70 and less than 80
elseif percent>=70 && percent<80
letter='C'
return
%if percent is greater than equal to 60 and less than 70
elseif percent>=60 && percent<70
letter='D'
return
%if percent is less than 60 and greater than 0
elseif percent<60 && percent>=0
letter='F'
return
%if percent is not numberic or less than 0(i.e negative number)
elseif isnumeric(percent)==0 || percent<0
error('Number is not accepted') %calling error function
end
endfunction
percent=input("Enter a number: ") %reading input from user
letter=grade(percent) %calling grade() function
fprintf('The final grade is: %s',letter) %printing final
grade
CODE SCREENSHOT:
OUTPUT: