PL/SQL Program to Check Number is Odd or Even

In this article, You will be able to understand how to write a PL/SQL Program to Check Number is Odd or Even.

An even number may be a number that will be divided into two equal groups. An odd number must be a number that can’t be divided into two equal groups.

Even numbers end in 2, 4, 6, 8, and 0 no matter what percentage digits they need (we know the amount 3,817,624 is even because it ends during a 4!). Odd numbers end with 1, 3, 5, 7, 9,11,13.

This is one of the commonly asked interview questions which is frequently asked as Write a PL/SQL program for a given number is even or odd during your interviews.

PL/SQL Program to Check Number is Odd or Even

declare
n number:=&n;
 
begin
if mod(n,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;
/

The below ER diagram helps us to understand the process flow. How to print odd or even numbers using the PLSQL program.

The first user must specify the number which we are going to print at the end. As a next step, we need to check whether it is divisible by two or not.

It gives the output either zero or a value.

If the returned value is zero obviously it is an even number. Why because we know all even numbers divisible by zero.

In case the program returned value is not a zero we can consider it is not divisible by zero hence it is an odd number.

Hope you are clear with this part.

plsql program check number odd even

Output

Enter a value for n: 8
old 2: n number:=&n;
new 2: n number:=8;
number is even

Explanation

When you give the input value as 8 to the PLSQL program it takes the input and validates it with our program.

We have defined a step where it has to check the mod of a given number

mod(8,2)=0 

Introduction:
In the realm of programming, determining whether a given number is odd or even is a common task. PL/SQL, a procedural language specifically designed for Oracle databases, offers a powerful set of tools to accomplish this task efficiently.

Code:

DECLARE
  input_number NUMBER;
BEGIN
  -- Prompt the user for input
  input_number := &input_number;

  -- Check if the number is odd or even
  IF MOD(input_number, 2) = 0 THEN
    DBMS_OUTPUT.PUT_LINE('The number is even.');
  ELSE
    DBMS_OUTPUT.PUT_LINE('The number is odd.');
  END IF;
END;
/

Explanation:

  1. DECLARE: This keyword indicates the start of the declaration section where we define variables.
  2. input_number NUMBER;: This line declares a variable named input_number of the NUMBER data type, which will store the user input.
  3. BEGIN: This keyword signifies the beginning of the executable section.
  4. input_number := &input_number;: This line prompts the user to enter a number and assigns the input value to the input_number variable. The & symbol is used to retrieve the value entered by the user.
  5. IF MOD(input_number, 2) = 0 THEN: This conditional statement checks if the remainder of dividing input_number by 2 is equal to 0. If true, it means the number is even.
  6. DBMS_OUTPUT.PUT_LINE('The number is even.');: If the number is even, this line uses the DBMS_OUTPUT.PUT_LINE procedure to display the message “The number is even.” on the console.
  7. ELSE: If the condition in the previous IF statement is false, the program execution continues here.
  8. DBMS_OUTPUT.PUT_LINE('The number is odd.');: This line is executed when the number is odd. It uses the DBMS_OUTPUT.PUT_LINE procedure to display the message “The number is odd.” on the console.
  9. END IF;: This statement marks the end of the conditional block.
  10. END;: This keyword marks the end of the PL/SQL block.
  11. /: The forward slash is used to execute the PL/SQL block.

You May Also Like: