Home » SQL & PL/SQL » SQL & PL/SQL » Fibonacci Series
Fibonacci Series Wed, 08 November 2006 23:02
 romi Messages: 67Registered: October 2006 Member

Hello Friends,

I want to print Fibonacci Series like :-

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

How can i do it.Can i do it by procedure or function?
Re: Fibonacci Series [message #202298 is a reply to message #202281] Thu, 09 November 2006 00:43
 abhijitrai82 Messages: 1Registered: November 2006 Location: New Delhi Junior Member
Hi Romi
You can try with the procedure given below
<code>
CREATE OR REPLACE procedure Fibonacci_Series(n number)is
fs1 number := 0;
fs2 number := 1;
fs3 number;
cnt number default 0;
t number;
begin
dbms_output.put_line(fs1);
dbms_output.put_line(fs2);
while cnt!= n-2
loop
fs3:=fs1+fs2;
dbms_output.put_line(fs3);
t:=fs2;
fs2:=fs3;
fs1:=t;
cnt:=cnt+1;
end loop;
exception
when others then
dbms_output.put_line(sqlerrm);
end;
</code>
n is number of terms in your series which you have to give explicitly through running the procedure.
Re: Fibonacci Series [message #202307 is a reply to message #202298] Thu, 09 November 2006 01:13
 Maaher Messages: 7062Registered: December 2001 Senior Member

MHE
Re: Fibonacci Series [message #539469 is a reply to message #202281] Mon, 16 January 2012 04:04
 prabhush Messages: 1Registered: January 2012 Location: Chennai Junior Member
Hi,

Try the below procedure and it will surely work whatever value even -ve values say Fibonacci(-5) will accept and throw error

CREATE OR REPLACE procedure Fibonacci(n number)is
a number:=0;
b number:=1;
c number;
i number;
t number;
prabhu exception;
begin
if n<=0 then
raise prabhu;
elsif n=1 then
dbms_output.put_line(a);
else
dbms_output.put_line(a);
dbms_output.put_line(b);
for i in 3..n loop
c:=a+b;
dbms_output.put_line(c);
a:=b;
b:=c;
end loop;
end if;
exception
when prabhu then
dbms_output.put_line('N value is invalid');
when others then
dbms_output.put_line(sqlerrm);
end;

once you compile the above do as below

begin
Fibonacci(6);
end;

O/P will be :

0
1
1
2
3
5

Statement processed.

Try and tell me how much you satisfied

[Updated on: Mon, 16 January 2012 04:06]

Report message to a moderator

Re: Fibonacci Series [message #539476 is a reply to message #539469] Mon, 16 January 2012 04:48
 Michel Cadot Messages: 65143Registered: March 2007 Location: Nanterre, France, http://... Senior MemberAccount Moderator
Welcome to the forum.

Make sure that lines of code do not exceed 80 characters when you format.
Indent the code, use code tags and align the columns in result.
Use the "Preview Message" button to verify.

See the following topic: Puzzle n°10 - All possible solution for Fibonacci Series

Regards
Michel
 Previous Topic: Global exception in package available outside package Next Topic: change week startday to tuesday
Goto Forum:

Current Time: Fri Aug 18 13:26:29 CDT 2017

Total time taken to generate the page: 0.09527 seconds