PL/SQL function To print the Fibonacci series
Following function will print the Fibonacci series:
CREATE OR REPLACE FUNCTION fib (n POSITIVE) RETURN INTEGER IS
BEGIN
IF (n = 1) OR (n = 2) THEN -- terminating condition
RETURN 1;
ELSE
RETURN fib(n - 1) + fib(n - 2); -- recursive call
END IF;
END fib;
/
-- Test Fibonacci Series:
SELECT fib(1), fib(2), fib(3), fib(4), fib(5) FROM dual;
===================================================
Following programs will Print the Fibonacci series:
DECLARE
RESULT NUMBER := 1;
previous NUMBER := -1;
l_sum NUMBER;
n NUMBER;
l_in NUMBER := 10;
BEGIN
FOR n IN 1 .. l_in
LOOP
l_sum := RESULT + previous;
previous := RESULT;
RESULT := l_sum;
DBMS_OUTPUT.put_line (l_sum);
END LOOP;
END;
Following function will print the Fibonacci series:
CREATE OR REPLACE FUNCTION fib (n POSITIVE) RETURN INTEGER IS
BEGIN
IF (n = 1) OR (n = 2) THEN -- terminating condition
RETURN 1;
ELSE
RETURN fib(n - 1) + fib(n - 2); -- recursive call
END IF;
END fib;
/
-- Test Fibonacci Series:
SELECT fib(1), fib(2), fib(3), fib(4), fib(5) FROM dual;
===================================================
Following programs will Print the Fibonacci series:
DECLARE
RESULT NUMBER := 1;
previous NUMBER := -1;
l_sum NUMBER;
n NUMBER;
l_in NUMBER := 10;
BEGIN
FOR n IN 1 .. l_in
LOOP
l_sum := RESULT + previous;
previous := RESULT;
RESULT := l_sum;
DBMS_OUTPUT.put_line (l_sum);
END LOOP;
END;
No comments:
Post a Comment