Factorial: The product of an integer and all the integers below it; e.g. factorial four ( 4! ) is equal to:
4*3*2*1=24
To put it in an Oracle PL/SQL code, I would create a function that will take the input of an integer whose factorial is to be calculated and the return value would be the factorial of the given input.
CREATE OR REPLACE FUNCTION GET_FACTORIAL (P_INT NUMBER) RETURN NUMBER IS P_FACT PLS_INTEGER := 1; BEGIN FOR I IN 1 .. P_INT LOOP P_FACT := P_FACT * I; END LOOP; RETURN P_FACT; END; /
The same can now be executed from a select statement as follows:
SELECT GET_FACTORIAL(6) FROM DUAL; Output: 720