Check whether a given number is even or odd in PL/SQL
Last Updated :
02 Jul, 2018
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
n NUMBER := 1634;
r NUMBER;
BEGIN
r := MOD(n, 2);
IF r = 0 THEN
dbms_output.Put_line( 'Even' );
ELSE
dbms_output.Put_line( 'Odd' );
END IF;
END ;
|
Output:
Even
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...