Here’s a PL/SQL program to find the reverse of a number:
DECLARE
num NUMBER;
reverse_num NUMBER := 0;
remainder NUMBER;
BEGIN
-- Prompt the user for input
num := #
-- Store the original number in a temporary variable
DECLARE
original_num NUMBER := num;
BEGIN
-- Calculate the reverse of the number
WHILE num > 0 LOOP
remainder := num MOD 10;
reverse_num := reverse_num * 10 + remainder;
num := FLOOR(num / 10);
END LOOP;
END;
-- Display the reverse of the number
DBMS_OUTPUT.PUT_LINE('The reverse of ' || original_num || ' is: ' || reverse_num);
END;
/
Explanation:
DECLARE
: This keyword indicates the start of the declaration section where we define variables.num NUMBER;
: This line declares a variable namednum
of the NUMBER data type, which will store the original number.reverse_num NUMBER := 0;
: This line initializes the variablereverse_num
to 0. It will store the reverse of the original number.remainder NUMBER;
: This line declares a variable namedremainder
of the NUMBER data type. It will store the remainder when dividing the number by 10.BEGIN
: This keyword signifies the beginning of the executable section.num := #
: This line prompts the user to enter the number and assigns the input value to thenum
variable. The&
symbol is used to retrieve the value entered by the user.DECLARE
: This keyword indicates the start of the nested block where we define another variable to store the original number.original_num NUMBER := num;
: This line declares a variable namedoriginal_num
of the NUMBER data type and assigns it the value ofnum
. This variable will store the original number for display purposes.BEGIN
: This keyword signifies the beginning of the nested block’s executable section.WHILE num > 0 LOOP
: This loop will execute as long as the value ofnum
is greater than 0.remainder := num MOD 10;
: This line calculates the remainder whennum
is divided by 10 and assigns it to theremainder
variable.reverse_num := reverse_num * 10 + remainder;
: This line updates thereverse_num
variable by multiplying it by 10 and adding the value ofremainder
. This effectively builds the reverse of the number.num := FLOOR(num / 10);
: This line updates the value ofnum
by dividing it by 10 and taking the floor of the result. This effectively removes the rightmost digit fromnum
in each iteration.END LOOP;
: This statement marks the end of the loop.DBMS_OUTPUT.PUT_LINE('The reverse of ' || original_num || ' is: ' || reverse_num);
: This line uses theDBMS_OUTPUT.PUT_LINE
procedure to display the reverse of the original number. It concatenates the values oforiginal_num
andreverse_num
to form a descriptive message.
By running this PL/SQL program, you can find the reverse of a given number. The program prompts the user to enter the number, calculates the reverse of the number using a loop, and then displays the reverse on the console. This allows you to quickly obtain the reverse of the given number.