Write a function to accept three sides of a triangle

Q. Write a PL/SQL function to accept three parameters,

I) The sides of a triangle that should be user's choice.

II) The function should return a Boolean value, 'true' if the triangle is valid, else 'false' .

III) Check if the dimensions entered by the user can form a valid triangle. (A triangle is valid if the length of each side is less than the sum of the lengths of the other two sides.)


Answer:

Exception Code:

The following code will check if a triangle is valid or not.

CREATE OR REPLACE FUNCTION triangle(a number,b number,c number) RETURN boolean AS invalid_triangle exception;
BEGIN IF NOT (a+b>=c
               AND b+c>=a
               AND c+a>=b) THEN RAISE invalid_triangle;
ELSE RETURN TRUE;
END IF;
exception WHEN invalid_triangle THEN dbms_output.put_line('xxxxxx- invalid triangle -xxxxxxxxxx');
RETURN FALSE;
WHEN others THEN dbms_output.put_line('un identified error occured');
END;
/


Calling Code:

DECLARE a number:=&side1;
b number:=&side2;
c number:=&side3;
x boolean;
BEGIN x:=triangle(a,b,c);
END;
/


Output:

triangle valid