Skip to main content

more options

Self-check exercise: Conditionals

  1. The three interior angles of any triangle add up to 180°. Assume the three angles of a triangle are stored in variables a, b and c. Write a program fragment to print 'scalene', 'isoceles', or 'equilateral', according to whether all angles are different, there is a pair of angles that are equal, all or angles are equal, respectively.

  2. The statement c = rand; assings a uniform random number in the interval (0,1) to c. Write a fragment of code to simulate the flipping of a fair coin. That is, write a peice of code that prints 'heads' or 'tails' with equal probability.

  3. Write three different programs to determine in which quadrant a user-input value of A degrees belongs. Assume that the user may enter any non-negative number. For example, 725° is the same, and should be treated, as 5°. (Hint: the function rem might be useful.) To avoid ambiguity, we use the following convention:

    Quadrant is if
    1 0<= A < 90
    2 90<= A < 180
    3 180<= A < 270
    4 270<= A < 360

    Print the result. The specifications for each of the three scripts are as follows:

    1. In the first script use four separate if statements (4 separate if-end constructs).
    2. In the second script, use a single if-elseif-else-end construction for the evaluation.
    3. In the third script, use nesting without using the elseif clause and call it angle3.m.

    Pay close attention to the differences among the three programs.