PL/SQL Program for Fibonacci Series

Here you will get pl/sql program for fibonacci series.

It is a series in which next number is the sum of previous two numbers.

 

PL/SQL Program for Fibonacci Series

declare
	first number:=0;
	second number:=1;
	third number;
	n number:=&n;
	i number;

begin
	dbms_output.put_line('Fibonacci series is:');
	dbms_output.put_line(first);
	dbms_output.put_line(second);	

	for i in 2..n
	loop
		third:=first+second;
		first:=second;
		second:=third;
		dbms_output.put_line(third);
	end loop;
end;
/

 

Output

Enter value for n: 6
old 5: n number:=&n;
new 5: n number:=6;
Fibonacci series is:
0
1
1
2
3
5
8

7 thoughts on “PL/SQL Program for Fibonacci Series”

    1. as we have already printed first and second number, we should use (3..n) to generate 5 terms(0 1 2 3 5).

    1. create or replace function fn1 (n in number) return sys_refcursor as
      first number:=5;
      third number;
      i number;

      begin
      dbms_output.put_line(first);

      for i in 3..n
      loop
      third:=first+i;
      first:=third;
      dbms_output.put_line((third));
      end loop;
      end;
      /

    1. create or replace function fn1 (n in number) return sys_refcursor as
      first number:=0;
      second number:=1;
      third number;

      begin
      dbms_output.put_line(‘Fibonacci series is:’);
      dbms_output.put_line(first);
      dbms_output.put_line(second);

      for i in 2..n
      loop
      third:=first+second;
      first:=second;
      second:=third;
      dbms_output.put_line(third);
      end loop;
      end;
      /

Leave a Comment

Your email address will not be published. Required fields are marked *