Fibonacci sequence in Oracle sql

Fibonacci sequence in Oracle sql

Write sql query to generate Fibonacci sequence in Oracle sql?

No, Oracle sql query doesn''t support single query to get the fabinocci sequence .

Becuase Oracle SQL is basically for data manipulationa and querying  and it is not fr generating sequence or peforming iterative calculaiation like Fibonacci sequence.

The Fibonacci sequence define as which each number is the sum of the two preceding ones.

we call it Fibonacci numers those are part of series.

Example

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55

we would need a PL/SQL code to achieve fibonacci sequence.

Below is the example.

PLSQL Code


DECLARE

    n1 NUMBER := 0;

    n2 NUMBER := 1;

    n3 NUMBER;

BEGIN

    DBMS_OUTPUT.PUT_LINE(n1);

    DBMS_OUTPUT.PUT_LINE(n2);


    FOR i IN 2..10 LOOP

        n3 := n1 + n2;

        n1 := n2;

        n2 := n3;

        DBMS_OUTPUT.PUT_LINE(n3);

    END LOOP;

END;






This program uses a loop to calculate and print the Fibonacci sequence up to 100. The initial values of `n1` and `n2` are set to 0 and 1, respectively. The loop then calculates the next Fibonacci number (`n3`) by adding `n1` and `n2`, and updates the values of `n1` and `n2`. Finally, the program outputs the Fibonacci numbers using the `DBMS_OUTPUT.PUT_LINE` function.

Post a Comment

0 Comments