PL/SQL Program for Prime Number

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:

  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 to check (n) and a "flag" variable (is_prime) to keep track of whether we've found a divisor. A BOOLEAN (TRUE/FALSE) variable is perfect for this.
  4. FOR Loop: We will loop from 2 up to half of the number's value to check for divisors.
  5. MOD Function: This is the key. MOD(n, i) gives the remainder of n divided by i. If the remainder is 0, it means i is a divisor, and the number is not prime.
  6. IF...THEN...ELSE Logic: 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

  1. DECLARE section: We create n and set it to 17. We also create a BOOLEAN flag is_prime and set its default to TRUE. Our logic will try to prove this wrong.
  2. BEGIN section: The program's logic starts.
  3. IF n <= 1 THEN: This is our first check. The numbers 0 and 1 are not prime, so if n is 1 or less, we set is_prime to FALSE.
  4. ELSE: If n is 2 or greater, we proceed to check for divisors.
  5. 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 of n (using TRUNC to get a whole number). There's no need to check any number larger than n/2.
  6. IF MOD(n, i) = 0 THEN: Inside the loop, we check if n is perfectly divisible by i.
    • For n = 17, it checks MOD(17, 2), MOD(17, 3), ... MOD(17, 8). None of these return 0.
    • For n = 18, the first check is MOD(18, 2), which is 0.
  7. is_prime := FALSE; EXIT;: If we find a divisor (like 2 for 18), we immediately set our flag is_prime to FALSE and use EXIT to quit the loop. There is no point in checking 3, 4, 5, etc., as we already know the number is not prime.
  8. IF is_prime THEN ...: After the END IF; and END LOOP;, the program checks the final value of the is_prime flag and prints the correct message.
Vinish Kapoor
Vinish Kapoor

Vinish Kapoor is a seasoned software development professional and a fervent enthusiast of artificial intelligence (AI). His impressive career spans over 25+ years, marked by a relentless pursuit of innovation and excellence in the field of information technology. As an Oracle ACE, Vinish has distinguished himself as a leading expert in Oracle technologies, a title awarded to individuals who have demonstrated their deep commitment, leadership, and expertise in the Oracle community.

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments