SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

Factorial of a number in PL/SQL

To calculate the factorial of a number using PL/SQL, you can make use of a simple iterative approach or a recursive method. Here, I'll demonstrate both approaches.

  • Iterative Approach:
DECLARE 
    n NUMBER := 5; -- Change this value for different results
    factorial NUMBER := 1;
    i NUMBER;
BEGIN 
    FOR i IN 1..n LOOP
        factorial := factorial * i;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('Factorial of ' || n || ' is: ' || factorial);
END;
/
  • Recursive Approach: To implement the recursive approach, you can define a function that calls itself. Here's an example:
DECLARE 
    FUNCTION factorial_recursive(n NUMBER) RETURN NUMBER IS
    BEGIN 
        IF n = 0 THEN
            RETURN 1;
        ELSE
            RETURN n * factorial_recursive(n-1);
        END IF;
    END factorial_recursive;

    num NUMBER := 5; -- Change this value for different results
BEGIN 
    DBMS_OUTPUT.PUT_LINE('Factorial of ' || num || ' is: ' || factorial_recursive(num));
END;
/

For both scripts, you can change the value of n or num to compute the factorial for a different number. However, be cautious with the recursive approach for larger values as it might exceed the maximum recursion depth. The iterative approach is generally more efficient for calculating factorial in such cases.

  1. PL/SQL Procedure for Factorial Computation:

    CREATE OR REPLACE PROCEDURE CalculateFactorial(p_number IN NUMBER) IS
        v_result NUMBER := 1;
    BEGIN
        FOR i IN 1..p_number LOOP
            v_result := v_result * i;
        END LOOP;
        DBMS_OUTPUT.PUT_LINE('Factorial of ' || p_number || ' is: ' || v_result);
    END;
    /
    
  2. PL/SQL Function for Factorial Computation:

    CREATE OR REPLACE FUNCTION Factorial(p_number IN NUMBER) RETURN NUMBER IS
        v_result NUMBER := 1;
    BEGIN
        FOR i IN 1..p_number LOOP
            v_result := v_result * i;
        END LOOP;
        RETURN v_result;
    END;
    /
    
  3. PL/SQL Program to Find Factorial of a Given Number:

    DECLARE
        v_number NUMBER := 5; -- Change this to the desired number
        v_result NUMBER;
    BEGIN
        v_result := Factorial(v_number);
        DBMS_OUTPUT.PUT_LINE('Factorial of ' || v_number || ' is: ' || v_result);
    END;
    /
    

Oracle SQL Function for Finding Factorial:

  1. Oracle SQL Query to Find Factorial of a Number:

    WITH RecursiveFactorial AS (
        SELECT 5 AS n, 1 AS factorial
        FROM dual
        UNION ALL
        SELECT n - 1, factorial * (n - 1)
        FROM RecursiveFactorial
        WHERE n > 1
    )
    SELECT factorial
    FROM RecursiveFactorial
    WHERE n = 1;
    
  2. Oracle SQL Script to Calculate Factorial:

    DECLARE
        v_number NUMBER := 5; -- Change this to the desired number
        v_result NUMBER;
    BEGIN
        SELECT factorial
        INTO v_result
        FROM (
            SELECT n, factorial
            FROM (
                SELECT LEVEL AS n, 1 AS factorial
                FROM dual
                CONNECT BY LEVEL <= v_number
            )
            CONNECT BY PRIOR n = n - 1
        )
        WHERE n = v_number;
    
        DBMS_OUTPUT.PUT_LINE('Factorial of ' || v_number || ' is: ' || v_result);
    END;
    /