PL/SQL Program to Check Number is Odd or Even

Determining if a number is odd or even is a fundamental programming exercise. In PL/SQL, this is very easy to do and is the perfect way to learn about conditional logic (IF...THEN...ELSE) and the MOD function.

This simple guide will show you the logic and a complete program to solve this problem.

What is the Logic for Odd or Even?

The logic is based on a simple mathematical rule:

  • An even number is a whole number that is perfectly divisible by 2 (it has a remainder of 0).
  • An odd number is a whole number that has a remainder of 1 when divided by 2.

In Oracle, the MOD(n, 2) function is the perfect tool for this. It returns the remainder of n divided by 2.

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 we want to check (e.g., n NUMBER := 17;).
  4. MOD Function: The key to the program. MOD(n, 2) will return 0 if n is even and 1 if n is odd.
  5. IF...THEN...ELSE Logic: We'll use this to check the result of the MOD function and print the correct message.
  6. Printing the Result: We use DBMS_OUTPUT.PUT_LINE() to print the answer.

PL/SQL Program: Check for Odd or Even

This program will check the number stored in the n variable and print whether it is odd or even.

PL/SQL Program

SET SERVEROUTPUT ON;

DECLARE
  -- The number we want to test
  n NUMBER := 17; 
  
BEGIN
  
  -- Check if the remainder (MOD) of n / 2 is 0
  IF MOD(n, 2) = 0 THEN
  
    -- If the remainder is 0, the number is even
    DBMS_OUTPUT.PUT_LINE(n || ' is an EVEN number.');
    
  ELSE
  
    -- If the remainder is anything else (like 1), the number is odd
    DBMS_OUTPUT.PUT_LINE(n || ' is an ODD number.');
    
  END IF;

END;
/

Result (for n := 17)

17 is an ODD number.

Result (if you change to n := 20)

20 is an EVEN number.

Program Explanation

  1. DECLARE section: We create one variable, n, and assign it the value 17.
  2. BEGIN section: The program's logic starts.
  3. IF MOD(n, 2) = 0 THEN: This is the main check.
    • The program first calculates MOD(17, 2), which returns the remainder 1.
    • It then checks if 1 = 0. This is FALSE.
  4. ELSE: Because the IF condition was FALSE, the program skips the THEN block and runs the ELSE block.
  5. DBMS_OUTPUT.PUT_LINE(...): The program prints the string '17 is an ODD number.'.
  6. END IF; and END;: The IF block and the main program block are closed.
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