Infolinks

Thursday 21 June 2012

PL/SQL funtion to compute the factorial of a number

PL/SQL funtion to compute the factorial of  a number


Following PL/SQL funtion can be used to compute the factorial of  a number:


CREATE OR REPLACE FUNCTION fac (n POSITIVE) RETURN INTEGER IS
BEGIN
IF n = 1 THEN -- terminating condition
RETURN 1;
ELSE
RETURN n * fac(n - 1); -- recursive call
END IF;
END fac;
/

-- Test n!
SELECT fac(1), fac(2), fac(3), fac(4), fac(5) FROM dual;

No comments:

Post a Comment