Open In App

Program for Fibonacci numbers in PL/SQL

Last Updated : 13 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report


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:




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


Output:

0 1 1 2 3 5


Next Article
Article Tags :
Practice Tags :

Similar Reads