ICA 18-18 Write a program that asks the user to enter the length of the hypotenu
ID: 2088590 • Letter: I
Question
ICA 18-18 Write a program that asks the user to enter the length of the hypotenuse, opposite, and adjacent sides of a right triangle with respect to an angle (?). After the user has entered all three values, a menu should pop up asking if the user wants to calculate sin(0), cos(0), tan(8) cot(0), cosec(0), or sec(0). The program should display a final message, such as, "For a right triangle of sides h, o and a, sin(?) -0.371, " where h, o, and a are replaced by the actual lengths entered by the user, displayed to one decimal place. If the user enters lengths that do not work with a right triangle, an error message should appear and the program should terminate. If the user closes the menu instead of making a valid selection, an appropriate error message should appear and the program should terminate. You may use if statements and/or switch-case statements as appropriate. opp adj Note: A great way to check to see if the triangle is a valid right triangle is to calculate s and x. a +b+c) 2 Ists -as-bs-.Explanation / Answer
function trigonometry
a = input('Enter the length of hypotenuse of the triangle ');
b = input('Enter the length of opposite side of the triangle ');
c = input('Enter the length of adjacent side of the triangle ');
s = (a + b + c)/2
A = sqrt(s*(s-a)*(s-b)*(s-c)) % area of triangle
while isreal(A)~= 1
%error('sides entered does not make a triangle, Enter the sides again');
a = input('Enter the length of hypotenuse of the triangle ');
b = input('Enter the length of opposite side of the triangle ');
c = input('Enter the length of adjacent side of the triangle ');
end
t = asin(A/(a*c))*180/pi % angle theta (degree)
x = input('What do you want to calculate, sin(t), cos(t), tan(t),cot(t), sec(t) or cosec(t)? e.g. enter just "sin" if you want to get value of sin(t) ');
if x == 'sin'
fprintf('For a right angle triangle of sides %4.2f,%4.2f and %4.2f, %s (t)= %4.2f',a,b,c,x,double((b/a)));
elseif x == 'cos'
fprintf('For a right angle triangle of sides %4.2f,%4.2f and %4.2f, %s (t)= %4.2f',a,b,c,x,(c/a));
elseif x == 'tan'
fprintf('For a right angle triangle of sides %4.2f,%4.2f and %4.2f, %s (t)= %4.2f',a,b,c,x,(b/c));
elseif x == 'cot'
fprintf('For a right angle triangle of sides %4.2f,%4.2f and %4.2f, %s (t)= %4.2f',a,b,c,x,(c/b));
elseif x == 'sec'
fprintf('For a right angle triangle of sides %4.2f,%4.2f and %4.2f, %s (t)= %4.2f',a,b,c,x,(a/c));
elseif x == 'cosec'
fprintf('For a right angle triangle of sides %4.2f,%4.2f and %4.2f, %s (t)= %4.2f',a,b,c,x,(a/b));
else error('wrong input ');
end
end