PL/SQL Program for Fibonacci Series

The Fibonacci series is a famous mathematical sequence where each new number is the sum of the two preceding ones. Writing a program to generate this sequence is a fantastic exercise for learning how to manage and "shift" variable values inside a loop.

The sequence always starts with 0 and 1. 0, 1, 1, 2, 3, 5, 8, 13, 21, ...

This simple guide will show you the logic and a complete PL/SQL program to generate this series.

What You Need to Know

To write this program, you will use a few basic PL/SQL concepts:

  1. Enabling Output: You must run this command once in your SQL tool to see the printed results:SET SERVEROUTPUT ON;
  2. Anonymous Block: We will write our code in a DECLARE...BEGIN...END; block.
  3. Variables: This is the most important part. We need:
    • n_limit: How many terms of the series to print (e.g., 10).
    • n1: The first number in the sequence, initialized to 0.
    • n2: The second number in the sequence, initialized to 1.
    • n_temp: A temporary variable to hold the new sum during our calculation.
  4. FOR Loop: We will use a loop to calculate each new term, starting from the third term up to our limit.

PL/SQL Program: Print the Fibonacci Series

This program will print the first n_limit terms of the Fibonacci series.

PL/SQL Program

SET SERVEROUTPUT ON;

DECLARE
  -- How many terms of the series to print
  n_limit NUMBER := 10; 
  
  -- Initialize the first two numbers of the series
  n1 NUMBER := 0;
  n2 NUMBER := 1;
  
  -- A temporary variable for swapping
  n_temp NUMBER;

BEGIN

  -- Handle edge cases
  IF n_limit < 1 THEN
    RETURN; -- Do nothing if the limit is less than 1
  END IF;

  -- 1. Print the first number (0)
  DBMS_OUTPUT.PUT_LINE(n1); 
  
  -- 2. Loop from the 2nd term up to the limit
  FOR i IN 2..n_limit LOOP
  
    -- Print the next number in the series (starts with 1)
    DBMS_OUTPUT.PUT_LINE(n2);
    
    -- 3. Calculate the *next* term (0 + 1 = 1)
    n_temp := n1 + n2;
    
    -- 4. "Shift" the numbers for the next iteration
    n1 := n2;      -- The 1st number becomes the 2nd number
    n2 := n_temp;  -- The 2nd number becomes the new sum
    
  END LOOP;

END;
/

Result (for n_limit := 10)

0
1
1
2
3
5
8
13
21
34

Program Explanation

  1. DECLARE section: We set our limit n_limit to 10. We initialize n1 to 0 and n2 to 1, which are the two "seed" values of the series.
  2. BEGIN section: The logic starts.
  3. IF n_limit < 1 THEN ...: A quick check to make sure we don't try to run the loop.
  4. DBMS_OUTPUT.PUT_LINE(n1);: Before the loop starts, we print the very first number, 0.
  5. FOR i IN 2..n_limit LOOP: We start a loop that will run from i=2 up to i=10 (9 times) to generate the rest of the terms.

Inside the loop (the "shifting" logic):

  • Loop 1 (i=2):
    • DBMS_OUTPUT.PUT_LINE(n2); -> Prints 1
    • n_temp := n1 + n2; -> n_temp := 0 + 1 = 1
    • n1 := n2; -> n1 is now 1
    • n2 := n_temp; -> n2 is now 1
  • Loop 2 (i=3):
    • DBMS_OUTPUT.PUT_LINE(n2); -> Prints 1
    • n_temp := n1 + n2; -> n_temp := 1 + 1 = 2
    • n1 := n2; -> n1 is now 1
    • n2 := n_temp; -> n2 is now 2
  • Loop 3 (i=4):
    • DBMS_OUTPUT.PUT_LINE(n2); -> Prints 2
    • n_temp := n1 + n2; -> n_temp := 1 + 2 = 3
    • n1 := n2; -> n1 is now 2
    • n2 := n_temp; -> n2 is now 3
  • Loop 4 (i=5):
    • DBMS_OUTPUT.PUT_LINE(n2); -> Prints 3
    • n_temp := n1 + n2; -> n_temp := 2 + 3 = 5
    • n1 := n2; -> n1 is now 3
    • n2 := n_temp; -> n2 is now 5

This process continues until the loop finishes, with n1 and n2 always holding the two most recent numbers in the series.

Vinish Kapoor
Vinish Kapoor

An Oracle ACE and software veteran with 25+ years of experience, passionate about AI and IT innovation.

guest

0 Comments
Oldest
Newest Most Voted