Home » SQL & PL/SQL » SQL & PL/SQL » Fibonacci Series
Fibonacci Series [message #202281] Wed, 08 November 2006 23:02 Go to next message
romi
Messages: 67
Registered: 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 Go to previous messageGo to next message
abhijitrai82
Messages: 1
Registered: 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 Go to previous messageGo to next message
Maaher
Messages: 7062
Registered: December 2001
Senior Member
Search the board. Please.

MHE
Re: Fibonacci Series [message #539469 is a reply to message #202281] Mon, 16 January 2012 04:04 Go to previous messageGo to next message
prabhush
Messages: 1
Registered: 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 Go to previous message
Michel Cadot
Messages: 64119
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Welcome to the forum.

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
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: Tue Dec 06 10:23:10 CST 2016

Total time taken to generate the page: 0.11155 seconds