Write a MATLAB script Classic program that Prompts it user t
Write a MATLAB script (Classic program) that: Prompts it user to enter a valid Integer literal Reads what the user types(unevaluated) into a string variable. Determine if the string is a valid Integer literal based on the following rules. Consists of only of the following characters: \'+\', \'-\', and the digits \"0\" through V The \'+\' or \'-\' character may only appear (once) as the first character Consists of at least 1 character other than the characters: \'+\' or \'-\' character. Display what the user entered and the determination to the screen is a reasonable report like format. Please enter a valid Integer literal: +is not a valid Integer literal Please enter a valid Integer:2 2 is a valid Integer literal Please enter a valid Integer literal: -42 -42 is a valid Integer literal
Solution
MATLAB code
x=input(\'Please enter a valid Integer literal :\',\'s\');
if x==\'+\' | x==\'-\'
ans=[x \' is not a valid integer literal\'];
disp(ans)
elseif isinteger(x)==0
ans=[x \' is a valid integer literal\'];
disp(ans)
end
output:
Please enter a valid Integer literal :+
+ is not a valid integer literal
Please enter a valid Integer literal :2
2 is a valid integer literal
Please enter a valid Integer literal :-42
-42 is a valid integer literal
