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.
Get more Vinish.dev tutorials on Google

See my articles more often when you search Google.

Vinish Kapoor
Vinish Kapoor

An Oracle ACE, author of four books on Oracle APEX, SQL and PL/SQL, and Oracle Forms, and a software developer building Oracle database applications since 2001.

guest

0 Comments
Oldest
Newest Most Voted