PL/SQL Program to Check Number is Odd or Even

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 variable num of type NUMBER, 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 of num divided by 2 is 0.
  • DBMS_OUTPUT.PUT_LINE('The given number is even');: Prints a message if num is even.
  • ELSE: If the number is not even, this block executes.
  • DBMS_OUTPUT.PUT_LINE('The given number is odd');: Prints a message if num 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

  1. The program prompts the user to input a number.
  2. The MOD function checks whether the number is divisible by 2.
  3. If the remainder is 0, the number is even; otherwise, it is odd.
  4. The result is printed using DBMS_OUTPUT.PUT_LINE.

Example Output

Download Courses for Just $2
🎉 These Courses Are FREE! Just Pay a Small $2 Maintenance Fee 🎉

🚀 Get These Premium Courses Now! 🚀

Tableau Course

$2 Maintenance Fee

⬇ Download

DevOps Course

$2 Maintenance Fee

⬇ Download

Snowflake DB Course

$2 Maintenance Fee

⬇ Download

Power BI Course

$2 Maintenance Fee

⬇ Download
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.

plsql program check number odd even

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.

You May Also Like: