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:
- Enabling Output: You must run this command once in your SQL tool to see the printed results:
SET SERVEROUTPUT ON; - Anonymous Block: We will write our code in a
DECLARE...BEGIN...END;block. - 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 to0.n2: The second number in the sequence, initialized to1.n_temp: A temporary variable to hold the new sum during our calculation.
FORLoop: 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
DECLAREsection: We set our limitn_limitto 10. We initializen1to 0 andn2to 1, which are the two "seed" values of the series.BEGINsection: The logic starts.IF n_limit < 1 THEN ...: A quick check to make sure we don't try to run the loop.DBMS_OUTPUT.PUT_LINE(n1);: Before the loop starts, we print the very first number,0.FOR i IN 2..n_limit LOOP: We start a loop that will run fromi=2up toi=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 1n_temp := n1 + n2;->n_temp := 0 + 1= 1n1 := n2;->n1is now 1n2 := n_temp;->n2is now 1
- Loop 2 (i=3):
DBMS_OUTPUT.PUT_LINE(n2);-> Prints 1n_temp := n1 + n2;->n_temp := 1 + 1= 2n1 := n2;->n1is now 1n2 := n_temp;->n2is now 2
- Loop 3 (i=4):
DBMS_OUTPUT.PUT_LINE(n2);-> Prints 2n_temp := n1 + n2;->n_temp := 1 + 2= 3n1 := n2;->n1is now 2n2 := n_temp;->n2is now 3
- Loop 4 (i=5):
DBMS_OUTPUT.PUT_LINE(n2);-> Prints 3n_temp := n1 + n2;->n_temp := 2 + 3= 5n1 := n2;->n1is now 3n2 := n_temp;->n2is now 5
This process continues until the loop finishes, with n1 and n2 always holding the two most recent numbers in the series.
