Program for Fibonacci Numbers in PL/SQL



Given ?n' numbers the task is to generate the Fibonacci numbers in PL/SQL starting from 0 to n, where the Fibonacci series of integers is in the form

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

Where integers 0 (1st place) and (2nd place) will have a fixed place after that, the previous two digits will be added to get the integer of the next place as shown below.

  • 0+1=1 (3rd place)
  • 1+1=2 (4th place)
  • 2+1=3 (5th place) and So on.

Formula

Sequence F(n) of the Fibonacci series will have a recurrence relation defined as ?

    Fn = Fn-1 + Fn-2

Where, F(0)=0 and F(1)=1 are always fixed.

PL/SQL is an Oracle product which is a combination of SQL and Procedural language(PL) introduced in early 90's. It was introduced for adding more features and functionalities to the simple SQL.

Example

 -- declare variable a = 0 for first digit in a series
 -- declare variable b = 0 for Second digit in a series
 -- declare variable temp for first digit in a series
declare
a number := 0;
b number := 1;
temp number;
n number := 10;
i number;
begin
 dbms_output.put_line('fibonacci series is :');
 dbms_output.put_line(a);
 dbms_output.put_line(b);
 for i in 2..n
 loop
temp:= a + b;
a := b;
b := temp;
dbms_output.put_line(temp);
 end loop;
end;

Output

fibonacci series is : 0 1 1 2 3 5 8 13 21 34
Updated on: 2024-12-24T17:56:51+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements