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 namednumof the NUMBER data type, which will store the original number.reverse_num NUMBER := 0;: This line initializes the variablereverse_numto 0. It will store the reverse of the original number.remainder NUMBER;: This line declares a variable namedremainderof 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 thenumvariable. 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_numof 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 ofnumis greater than 0.remainder := num MOD 10;: This line calculates the remainder whennumis divided by 10 and assigns it to theremaindervariable.reverse_num := reverse_num * 10 + remainder;: This line updates thereverse_numvariable 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 ofnumby dividing it by 10 and taking the floor of the result. This effectively removes the rightmost digit fromnumin 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_LINEprocedure to display the reverse of the original number. It concatenates the values oforiginal_numandreverse_numto 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.