Program for Fibonacci numbers in PL/SQL
Last Updated :
13 Mar, 2023
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
first number := 0;
second number := 1;
temp number;
n number := 5;
i number;
begin
dbms_output.put_line( 'Series:' );
dbms_output.put_line( first );
dbms_output.put_line( second );
for i in 2..n
loop
temp := first + second ;
first := second ;
second := temp ;
dbms_output.put_line( temp );
end loop;
end ;
|
Output:
0 1 1 2 3 5
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...