Write a MATLAB program that asks the user for an age and then classifies the age
ID: 3774571 • Letter: W
Question
Write a MATLAB program that asks the user for an age and then classifies the age according to the following scheme: If the user attempts to input an age less than zero or greater than 120 the program should display "error". If the user inputs an age less than 1, the program should display "baby", an age less than 13, the program should display "child" an age less than 20, the program should display "teenager", an age less than 60, the program should display "adult" and an age greater than 60 the program should display "senior".Explanation / Answer
Run using Matlab/Octave
Program:
x=input('Please enter your age : ');
if (x >0 && x<=1)
disp('Baby')
elseif(x >1 && x<=13)
disp('Child')
elseif(x >13 && x<=20)
disp('Teenager')
elseif(x >20 && x<=60)
disp('Adult')
elseif(x >60 && x<=120)
disp('Senior')
elseif(x >120 && x<0)
disp('Error')
else
disp('Error')
end
Output:
sh-4.3$ octave -qf --no-window-system demo.m
octave: X11 DISPLAY environment variable not set
octave: disabling GUI features
warning: function ./demo.m shadows a core library function
Please enter your age : 1
Baby
Please enter your age : 3
Child
Please enter your age : 15
Teenager
Please enter your age : 40
Adult
Please enter your age : 130
Error
Please enter your age : -10
Error