Search This Blog

Thursday, December 6, 2012

fibonacci in PL/SQL



create or replace  package  as

    procedure print_fibonacci ( n number);

end fibonacci;

create or replace package body fibonacci as

  function calculate_fibonacci ( n number) return number
  as
   begin
      if (n <= 1) then
      return n;
      else
          return calculate_fibonacci(n-1) + calculate_fibonacci(n-2);
      end if;
   end calculate_fibonacci;
 
  procedure print_fibonacci ( n number) as
   counter pls_integer := 1;
  begin
      loop
          dbms_output.put_line(counter-1||':'||calculate_fibonacci(counter-1));
          exit when counter > n;
          counter := counter +1;
      end loop;
  end print_fibonacci;

end fibonacci;