PL/SQL Program to Find Factorial of a Number

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:

  1. 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;
  2. Anonymous Block: We will write our code in a DECLARE...BEGIN...END; block.
  3. Variables: We'll need a variable for the number (n) and another to store the calculated result (factorial_result).
  4. FOR Loop: We will use a loop to perform the repeated multiplication.
  5. IF...ELSIF...ELSE Logic: We'll use this to handle the special cases for 0 and 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

  1. DECLARE section: We create n and set it to 6. We create factorial_result and initialize it to 1. This is critical; if we initialized it to 0, the result of the multiplication would always be 0.
  2. BEGIN section: The program's logic starts.
  3. IF n < 0 THEN: The first check handles negative numbers, for which factorial is not defined.
  4. ELSIF n = 0 THEN: This handles the special case for 0!, which is 1.
  5. ELSE: If n is a positive number, the program proceeds to the loop.
  6. FOR i IN 1..n LOOP: This is our main loop. It will run n times, with i taking the value of 1, then 2, then 3, up to 6.
  7. factorial_result := factorial_result * i;: This is where the calculation happens. The loop iterates as follows:
    • i=1: factorial_result = 1 * 1 = 1
    • i=2: factorial_result = 1 * 2 = 2
    • i=3: factorial_result = 2 * 3 = 6
    • i=4: factorial_result = 6 * 4 = 24
    • i=5: factorial_result = 24 * 5 = 120
    • i=6: factorial_result = 120 * 6 = 720
  8. END LOOP;: After the loop finishes, factorial_result holds the final value of 720.
  9. DBMS_OUTPUT.PUT_LINE(...): The program prints the final, formatted result.
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