16th Example - Factorial Value on PL/SQL



Factorial is one of the techniques in the mathematical sciences. A factorial value of a number is the product of the number (n) with the number minus one (n-1). Suppose the value to be searched of factorial is 5, the calculation will be done is 5x4x3x2x1 which will result in the value 120. The following PL / SQL block will do factorial calculation:

DECLARE

        n NUMBER;

        hasil NUMBER:=1;

BEGIN

        n := &input_n;

        FOR i IN 1..n LOOP

                        hasil := hasil * i;

        END LOOP;

        DBMS_OUTPUT.PUT_LINE('5! = '||hasil);

END;

/

Suppose input_n variable value is 5. FOR loop iteration will do 5 times iteration. The outcome variable in the loop will make the process of multiplication value of i (number of looping) multiplied by the value of the last 5 times. After the loop 5 times then the hasil variabel value will be 120.

No comments

Powered by Blogger.