pl/sql program to swap two numbers

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:

  1. DECLARE: This keyword indicates the start of the declaration section where we define variables.
  2. num NUMBER;: This line declares a variable named num of the NUMBER data type, which will store the original number.
  3. reverse_num NUMBER := 0;: This line initializes the variable reverse_num to 0. It will store the reverse of the original number.
  4. remainder NUMBER;: This line declares a variable named remainder of the NUMBER data type. It will store the remainder when dividing the number by 10.
  5. BEGIN: This keyword signifies the beginning of the executable section.
  6. num := #: This line prompts the user to enter the number and assigns the input value to the num variable. The & symbol is used to retrieve the value entered by the user.
  7. DECLARE: This keyword indicates the start of the nested block where we define another variable to store the original number.
  8. original_num NUMBER := num;: This line declares a variable named original_num of the NUMBER data type and assigns it the value of num. This variable will store the original number for display purposes.
  9. BEGIN: This keyword signifies the beginning of the nested block’s executable section.
  10. WHILE num > 0 LOOP: This loop will execute as long as the value of num is greater than 0.
  11. remainder := num MOD 10;: This line calculates the remainder when num is divided by 10 and assigns it to the remainder variable.
  12. reverse_num := reverse_num * 10 + remainder;: This line updates the reverse_num variable by multiplying it by 10 and adding the value of remainder. This effectively builds the reverse of the number.
  13. num := FLOOR(num / 10);: This line updates the value of num by dividing it by 10 and taking the floor of the result. This effectively removes the rightmost digit from num in each iteration.
  14. END LOOP;: This statement marks the end of the loop.
  15. DBMS_OUTPUT.PUT_LINE('The reverse of ' || original_num || ' is: ' || reverse_num);: This line uses the DBMS_OUTPUT.PUT_LINE procedure to display the reverse of the original number. It concatenates the values of original_num and reverse_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.