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