Program for Fibonacci numbers in PL/SQL


The Fibonacci numbers are the numbers in the following integer sequence.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

Fn = Fn-1 + Fn-1

with seed values

F0= 0 and F1 = 1.
Given a number n, print n-th Fibonacci Number.

Input  : n = 2
Output : 1

Input  : n = 9
Output : 34

Below is the required implementation:

filter_none

edit
close

play_arrow

link
brightness_4
code

declare
  
-- declare variable first = 0,
-- second = 1 and temp of datatype number
first number := 0;
second number := 1;
temp number;
  
n number := 5;
i number;
  
begin
  
    dbms_output.put_line('Series:');
  
--print first two term first and second
    dbms_output.put_line(first);
    dbms_output.put_line(second);
  
-- loop i = 2 to n
    for i in 2..n
    loop
        temp:=first+second;
  
first := second;
second := temp;
  
--print terms of fibonacci series
    dbms_output.put_line(temp);
end loop;
  
end;
--Program End

chevron_right


Output:

0 1 1 2 3 5
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.