PL/SQL Program for Reverse of a Number

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:

  1. Enabling Output: You must run this command once in your SQL tool to see the printed results:SET SERVEROUTPUT ON;
  2. Anonymous Block: We will write our code in a DECLARE...BEGIN...END; block.
  3. 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).
  4. WHILE Loop: 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.
  5. 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) is 3).
  6. TRUNC(n / 10): This math function cuts off the decimal. When you divide a number by 10 and TRUNCate it, you effectively remove the last digit (e.g., TRUNC(123 / 10) is 12).

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

  1. DECLARE section: We create v_num (12345), v_reverse (initialized to 0), and v_remainder.
  2. BEGIN section: The logic starts. We print the original number.
  3. WHILE v_num > 0 LOOP: The loop begins and will continue to run as long as v_num is positive.

Inside the loop:

  • Loop 1:
    • v_num = 12345
    • v_remainder = MOD(12345, 10) = 5
    • v_reverse = (0 * 10) + 5 = 5
    • v_num = TRUNC(12345 / 10) = 1234 (Loop continues)
  • Loop 2:
    • v_num = 1234
    • v_remainder = MOD(1234, 10) = 4
    • v_reverse = (5 * 10) + 4 = 54
    • v_num = TRUNC(1234 / 10) = 123 (Loop continues)
  • Loop 3:
    • v_num = 123
    • v_remainder = MOD(123, 10) = 3
    • v_reverse = (54 * 10) + 3 = 543
    • v_num = TRUNC(123 / 10) = 12 (Loop continues)
  • Loop 4:
    • v_num = 12
    • v_remainder = MOD(12, 10) = 2
    • v_reverse = (543 * 10) + 2 = 5432
    • v_num = TRUNC(12 / 10) = 1 (Loop continues)
  • Loop 5:
    • v_num = 1
    • v_remainder = MOD(1, 10) = 1
    • v_reverse = (5432 * 10) + 1 = 54321
    • v_num = TRUNC(1 / 10) = 0 (Loop stops)
  1. DBMS_OUTPUT.PUT_LINE(...): The program exits the loop and prints the final value of v_reverse, which is 54321.
Get more Vinish.dev tutorials on Google

See my articles more often when you search Google.

Vinish Kapoor
Vinish Kapoor

An Oracle ACE, author of four books on Oracle APEX, SQL and PL/SQL, and Oracle Forms, and a software developer building Oracle database applications since 2001.

guest

0 Comments
Oldest
Newest Most Voted