Writing a program to reverse a number (for example, turning 12345 into 54321) is a classic programming challenge. It's a great way to learn how to use loops and basic math functions like MOD and TRUNC to manipulate individual digits.
This simple guide will show you the logic and a complete PL/SQL program to solve this problem.
What You Need to Know
To write this program, you will use a few basic PL/SQL concepts:
- Enabling Output: You must run this command once in your SQL tool to see the printed results:
SET SERVEROUTPUT ON; - Anonymous Block: We will write our code in a
DECLARE...BEGIN...END;block. - Variables: We'll need a variable for the number to reverse (
v_num), a variable to hold the new reversed number (v_reverse), and a temporary variable to hold the last digit (v_remainder). WHILELoop: A WHILE loop is perfect for this. We will tell the program to keep running the loop while the original number is still greater than 0.MOD(n, 10): This math function gives us the remainder of a division. When you divide by 10, the remainder is always the last digit (e.g.,MOD(123, 10)is3).TRUNC(n / 10): This math function cuts off the decimal. When you divide a number by 10 andTRUNCate it, you effectively remove the last digit (e.g.,TRUNC(123 / 10)is12).
PL/SQL Program: Reverse a Number
This program will take the number stored in v_num, reverse its digits, and print the result.
PL/SQL Program
SET SERVEROUTPUT ON;
DECLARE
-- The number we want to reverse
v_num NUMBER := 12345;
-- A variable to hold the reversed number as we build it
v_reverse NUMBER := 0;
-- A temporary variable to hold the last digit
v_remainder NUMBER;
BEGIN
-- Create a copy of the original number for the output message
DBMS_OUTPUT.PUT_LINE('Original number: ' || v_num);
-- Loop as long as the number is greater than 0
WHILE v_num > 0 LOOP
-- 1. Get the last digit
-- Example: MOD(12345, 10) = 5
v_remainder := MOD(v_num, 10);
-- 2. Build the new reversed number
-- Example: v_reverse = (0 * 10) + 5 => 5
-- v_reverse = (5 * 10) + 4 => 54
v_reverse := (v_reverse * 10) + v_remainder;
-- 3. Remove the last digit from the original number
-- Example: TRUNC(12345 / 10) = 1234
v_num := TRUNC(v_num / 10);
END LOOP;
-- Print the final result
DBMS_OUTPUT.PUT_LINE('Reversed number: ' || v_reverse);
END;
/
Result (for n := 12345)
Original number: 12345
Reversed number: 54321
Program Explanation
DECLAREsection: We createv_num(12345),v_reverse(initialized to 0), andv_remainder.BEGINsection: The logic starts. We print the original number.WHILE v_num > 0 LOOP: The loop begins and will continue to run as long asv_numis positive.
Inside the loop:
- Loop 1:
v_num= 12345v_remainder=MOD(12345, 10)= 5v_reverse=(0 * 10) + 5= 5v_num=TRUNC(12345 / 10)= 1234 (Loop continues)
- Loop 2:
v_num= 1234v_remainder=MOD(1234, 10)= 4v_reverse=(5 * 10) + 4= 54v_num=TRUNC(1234 / 10)= 123 (Loop continues)
- Loop 3:
v_num= 123v_remainder=MOD(123, 10)= 3v_reverse=(54 * 10) + 3= 543v_num=TRUNC(123 / 10)= 12 (Loop continues)
- Loop 4:
v_num= 12v_remainder=MOD(12, 10)= 2v_reverse=(543 * 10) + 2= 5432v_num=TRUNC(12 / 10)= 1 (Loop continues)
- Loop 5:
v_num= 1v_remainder=MOD(1, 10)= 1v_reverse=(5432 * 10) + 1= 54321v_num=TRUNC(1 / 10)= 0 (Loop stops)
DBMS_OUTPUT.PUT_LINE(...): The program exits the loop and prints the final value ofv_reverse, which is54321.

