Writing a program to determine if a number is prime is a classic programming challenge. It's a great exercise to learn about loops, conditional logic (IF statements), and boolean flags in PL/SQL.
This simple guide will walk you through the logic and provide a complete program to solve this problem.
What is a Prime Number?
A prime number is a whole number greater than 1 that has only two divisors: 1 and itself.
- 7 is prime (only divisible by 1 and 7).
- 6 is not prime (divisible by 1, 2, 3, and 6).
- 1 is not a prime number (it only has one divisor).
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 to check (
n) and a "flag" variable (is_prime) to keep track of whether we've found a divisor. ABOOLEAN(TRUE/FALSE) variable is perfect for this. FORLoop: We will loop from 2 up to half of the number's value to check for divisors.MODFunction: This is the key.MOD(n, i)gives the remainder ofndivided byi. If the remainder is0, it meansiis a divisor, and the number is not prime.IF...THEN...ELSELogic: We'll use this to check for edge cases (like 1) and to print the final result.
PL/SQL Program: Check for a Prime Number
This program will check the number stored in the n variable and print whether it is prime or not.
PL/SQL Program
SET SERVEROUTPUT ON;
DECLARE
-- The number we want to test
n NUMBER := 17;
-- A "flag" to track the result. We assume it's prime until we prove otherwise.
is_prime BOOLEAN := TRUE;
BEGIN
-- First, handle the special cases.
-- Prime numbers must be greater than 1.
IF n <= 1 THEN
is_prime := FALSE;
ELSE
-- We only need to check for divisors up to half of the number's value
FOR i IN 2..TRUNC(n / 2) LOOP
-- Check if 'i' divides evenly into 'n'
IF MOD(n, i) = 0 THEN
-- If it does, 'n' is not prime.
is_prime := FALSE;
-- Exit the loop immediately. We don't need to check any more numbers.
EXIT;
END IF;
END LOOP;
END IF;
-- After all checks, print the final result
IF is_prime THEN
DBMS_OUTPUT.PUT_LINE(n || ' is a prime number.');
ELSE
DBMS_OUTPUT.PUT_LINE(n || ' is not a prime number.');
END IF;
END;
/
Result (for n := 17)
17 is a prime number.
Result (if you change to n := 18)
18 is not a prime number.
Program Explanation
DECLAREsection: We createnand set it to17. We also create aBOOLEANflagis_primeand set its default toTRUE. Our logic will try to prove this wrong.BEGINsection: The program's logic starts.IF n <= 1 THEN: This is our first check. The numbers 0 and 1 are not prime, so ifnis 1 or less, we setis_primetoFALSE.ELSE: Ifnis 2 or greater, we proceed to check for divisors.FOR i IN 2..TRUNC(n / 2) LOOP: This is our main loop. We start at 2 (the first possible divisor) and stop at half ofn(usingTRUNCto get a whole number). There's no need to check any number larger thann/2.IF MOD(n, i) = 0 THEN: Inside the loop, we check ifnis perfectly divisible byi.- For
n = 17, it checksMOD(17, 2),MOD(17, 3), ...MOD(17, 8). None of these return 0. - For
n = 18, the first check isMOD(18, 2), which is 0.
- For
is_prime := FALSE; EXIT;: If we find a divisor (like2for18), we immediately set our flagis_primetoFALSEand useEXITto quit the loop. There is no point in checking 3, 4, 5, etc., as we already know the number is not prime.IF is_prime THEN ...: After theEND IF;andEND LOOP;, the program checks the final value of theis_primeflag and prints the correct message.
