Calculating a factorial is a common programming exercise that demonstrates how to use loops for repeated multiplication. In PL/SQL, you can easily write a program to find the factorial of any given number.
This simple guide will show you the logic, the basic concepts, and a complete program to solve this problem.
What is a Factorial?
A factorial, denoted by an exclamation mark (e.g., 5!), is the product of all positive integers less than or equal to that number.
For example, the factorial of 5 (or 5!) is: 5! = 5 * 4 * 3 * 2 * 1 = 120
By definition:
- The factorial of 0 (or
0!) is 1. - Factorials are not defined for negative numbers.
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 (like SQL*Plus or SQL Developer) to see the printed results:
SET SERVEROUTPUT ON; - Anonymous Block: We will write our code in a
DECLARE...BEGIN...END;block. - Variables: We'll need a variable for the number (
n) and another to store the calculated result (factorial_result). FORLoop: We will use a loop to perform the repeated multiplication.IF...ELSIF...ELSELogic: We'll use this to handle the special cases for0and negative numbers.
PL/SQL Program: Find Factorial of a Number
This program will calculate the factorial of the number stored in the n variable and print the result.
PL/SQL Program
SET SERVEROUTPUT ON;
DECLARE
-- The number to find the factorial of
n NUMBER := 6;
-- A variable to hold the calculated result
-- We must initialize it to 1, not 0!
factorial_result NUMBER := 1;
BEGIN
-- First, handle the special cases
IF n < 0 THEN
DBMS_OUTPUT.PUT_LINE('Factorial is not defined for negative numbers.');
ELSIF n = 0 THEN
DBMS_OUTPUT.PUT_LINE('The factorial of 0 is: 1');
ELSE
-- This is the main logic for positive numbers
-- We loop from 1 up to n (e.g., 1, 2, 3, 4, 5, 6)
FOR i IN 1..n LOOP
-- Multiply the current result by the loop counter
factorial_result := factorial_result * i;
END LOOP;
-- Print the final result
DBMS_OUTPUT.PUT_LINE('The factorial of ' || n || ' is: ' || factorial_result);
END IF;
END;
/
Result (for n := 6)
The factorial of 6 is: 720
Program Explanation
DECLAREsection: We createnand set it to6. We createfactorial_resultand initialize it to1. This is critical; if we initialized it to0, the result of the multiplication would always be0.BEGINsection: The program's logic starts.IF n < 0 THEN: The first check handles negative numbers, for which factorial is not defined.ELSIF n = 0 THEN: This handles the special case for0!, which is1.ELSE: Ifnis a positive number, the program proceeds to the loop.FOR i IN 1..n LOOP: This is our main loop. It will runntimes, withitaking the value of1, then2, then3, up to6.factorial_result := factorial_result * i;: This is where the calculation happens. The loop iterates as follows:i=1:factorial_result=1 * 1=1i=2:factorial_result=1 * 2=2i=3:factorial_result=2 * 3=6i=4:factorial_result=6 * 4=24i=5:factorial_result=24 * 5=120i=6:factorial_result=120 * 6=720
END LOOP;: After the loop finishes,factorial_resultholds the final value of720.DBMS_OUTPUT.PUT_LINE(...): The program prints the final, formatted result.



