Write a MATLAB code, which prompts the user to input the name (character-type da
ID: 3890889 • Letter: W
Question
Write a MATLAB code, which prompts the user to input the name (character-type data) of a three dimensional geometric shape from the following list printed on the screen: - Cube - Sphere - Cylinder Based on the user’s input, your code, employing switch-case-end command, should further ask a user to enter the geometric properties of each shape (using input command) and display the volume within the following statement (using fprintf command): The volume of the given ?? is equal to ??.
Note: An error message should be displayed if a user enters an invalid shape.
Explanation / Answer
program:-
prompt1 = 'Enter cube or sphere or cylinder: ';
shape = input(prompt1,'s'); %ask user for Enter cube or sphere or cylinder and store it in shape
switch shape
case 'cube' %for cube
prompt = 'Enter side of the cube: '; %ask user for side of cube
side = input(prompt);
volume = side*side*side; %calculate volume of cube
fprintf('The volume of the given %s is equal to %f. ',shape,volume); %print result
case 'sphere'
prompt = 'Enter radius of the sphere: '; %ask user for side of sphere
radius = input(prompt);
volume = 4/3*pi*radius*radius*radius; %calculate volume of sphere
fprintf('The volume of the given %s is equal to %f. ',shape,volume); %print result
case 'cylinder'
prompt = 'Enter radius of cylinder: '; %ask user for radius of cylinder
radius = input(prompt);
prompt = 'Enter height of cylinder: '; %ask user for height of cylinder
height = input(prompt);
volume = pi*radius*radius*height; %calculate volume of cylinder
fprintf('The volume of the given %s is equal to %f. ',shape,volume); %print result
otherwise
warning(' ----invalid shape----');
end
output:-
>> SwitchCase
Enter cube or sphere or cylinder: cube
Enter side of the cube: 10
The volume of the given cube is equal to 1000.000000.
>> SwitchCase
Enter cube or sphere or cylinder: sphere
Enter radius of the sphere: 10
The volume of the given sphere is equal to 4188.790205.
SwitchCase
Enter cube or sphere or cylinder: cylinder
Enter radius of cylinder: 10
Enter height of cylinder: 5
The volume of the given cylinder is equal to 1570.796327.
SwitchCase
Enter cube or sphere or cylinder: cone
Warning: ----invalid shape----
> In SwitchCase (line 22)
>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.