Open In App

Check whether a given number is even or odd in PL/SQL

Prerequisite PL/SQL introduction
In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations.

Examples:



Input: 2 
Output: even

Input: 5
Output: odd

The approach is to divide the given number by 2 and if the remainder is 0 then the given number is even else odd.

Below is the required implementation:






DECLARE
    -- Declare variable n, s, r, len
    -- and m of datatype number
    n NUMBER := 1634;
    r NUMBER;
BEGIN
    -- Calculating modulo
    r := MOD(n, 2);
  
    IF r = 0 THEN
      dbms_output.Put_line('Even');
    ELSE
      dbms_output.Put_line('Odd');
    END IF;
END;
--End program 

Output:

Even
Article Tags :
SQL