Determining whether a given number is odd or even is a fundamental programming task. In this article, we will explore how to accomplish this using PL/SQL, the procedural language designed for Oracle databases.
What is an Odd or Even Number?
- Even numbers: These numbers can be divided into two equal groups and always end in 0, 2, 4, 6, or 8 (e.g., 24, 86, 1024).
- Odd numbers: These cannot be divided into two equal groups and always end in 1, 3, 5, 7, or 9 (e.g., 15, 73, 913).
Understanding this concept is crucial, as checking for odd or even numbers is a common interview question.
PL/SQL Program to Check if a Number is Odd or Even
DECLARE
num NUMBER := #
BEGIN
IF MOD(num, 2) = 0 THEN
DBMS_OUTPUT.PUT_LINE('The given number is even');
ELSE
DBMS_OUTPUT.PUT_LINE('The given number is odd');
END IF;
END;
/
Explanation of the Code
1. DECLARE Section
DECLARE
: Marks the beginning of the variable declaration.num NUMBER := #
: Declares a variablenum
of typeNUMBER
, prompting the user for input.
2. Executable Section (BEGIN…END)
BEGIN
: Starts the executable section.IF MOD(num, 2) = 0 THEN
: Checks whether the remainder ofnum
divided by 2 is 0.DBMS_OUTPUT.PUT_LINE('The given number is even');
: Prints a message ifnum
is even.ELSE
: If the number is not even, this block executes.DBMS_OUTPUT.PUT_LINE('The given number is odd');
: Prints a message ifnum
is odd.END IF;
: Marks the end of the conditional block.END;
: Ends the PL/SQL block./
: Executes the PL/SQL program.
How the Program Works
- The program prompts the user to input a number.
- The MOD function checks whether the number is divisible by 2.
- If the remainder is
0
, the number is even; otherwise, it is odd. - The result is printed using
DBMS_OUTPUT.PUT_LINE
.
Example Output
🚀 Get These Premium Courses Now! 🚀
Enter a value for num: 8
The given number is even
Enter a value for num: 13
The given number is odd
Practical Applications of Odd/Even Checks
🔹 Data Validation: Ensure user-entered values meet specific numeric criteria. 🔹 Statistical Analysis: Separate even and odd numbers in datasets. 🔹 Algorithm Optimization: Optimize loops and conditional logic in programming.

Learn More About PL/SQL
For a deeper understanding of PL/SQL and other database operations, visit Oracle’s official PL/SQL documentation.
Final Thoughts
Checking whether a number is odd or even is a simple yet essential operation in PL/SQL programming. By mastering this concept, you enhance your problem-solving skills and prepare for database-related coding challenges. For more in-depth SQL discussions, visit our SQL Queries Community. Additionally, you can explore a detailed guide on PL/SQL Best Practices to further enhance your expertise.