PL/SQL Program for Palindrome Number

A "palindrome" is a number (or word) that reads the same backward as it does forward. For example, 121, 535, and 7 are all palindrome numbers.

Writing a program to check for this is a great exercise because it combines two common tasks: reversing a number (which you've seen before) and then comparing it to the original.

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:
    • v_num: The number we are testing.
    • v_original_num: A copy of the original number, which we need for the final comparison.
    • v_reverse: A variable to build the reversed number, initialized to 0.
    • v_remainder: A temporary variable to hold the last digit.
  4. WHILE Loop: We use this loop to reverse the number.
  5. MOD and TRUNC: We use these math functions to get the last digit (MOD) and then remove it (TRUNC).
  6. IF...THEN...ELSE Logic: We use this at the end to compare the original number to its reversed version.

PL/SQL Program: Check for Palindrome Number

This program will check the number stored in v_num, reverse it, and then print whether it is a palindrome or not.

PL/SQL Program

SET SERVEROUTPUT ON;

DECLARE
  -- The number we want to test
  v_num NUMBER := 121; 
  
  -- 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;
  
  -- A variable to hold a copy of the original number
  v_original_num NUMBER;

BEGIN
  
  -- 1. Store a copy of the original number
  v_original_num := v_num;
  
  -- 2. Start the loop to reverse the number
  WHILE v_num > 0 LOOP
    
    -- Get the last digit (e.g., 121 -> 1)
    v_remainder := MOD(v_num, 10);
    
    -- Build the reversed number (e.g., 0*10 + 1 = 1)
    v_reverse := (v_reverse * 10) + v_remainder;
    
    -- Remove the last digit from the number (e.g., 121 -> 12)
    v_num := TRUNC(v_num / 10);
    
  END LOOP;
  
  -- 3. Compare the original number to its reversed version
  IF v_original_num = v_reverse THEN
    DBMS_OUTPUT.PUT_LINE(v_original_num || ' is a palindrome number.');
  ELSE
    DBMS_OUTPUT.PUT_LINE(v_original_num || ' is NOT a palindrome number.');
  END IF;

END;
/

Result (for n := 121)

121 is a palindrome number.

Result (if you change to n := 123)

123 is NOT a palindrome number.

Program Explanation

  1. DECLARE section: We create our variables. v_num is set to 121. v_original_num is not set yet.
  2. BEGIN section: The logic starts.
  3. v_original_num := v_num;: This is a critical step. We store a copy of 121 in v_original_num. We must do this because the WHILE loop will destroy the value in v_num (it will end up as 0).
  4. WHILE v_num > 0 LOOP: The loop begins. It will run as long as v_num is positive.
    • Loop 1: v_num=121, v_remainder=1, v_reverse=1, v_num=12
    • Loop 2: v_num=12, v_remainder=2, v_reverse=12, v_num=1
    • Loop 3: v_num=1, v_remainder=1, v_reverse=121, v_num=0
  5. END LOOP;: The loop stops because v_num is now 0.
  6. IF v_original_num = v_reverse THEN: This is the final check.
    • The program compares v_original_num (which is still 121) to v_reverse (which is now 121).
    • Since 121 = 121 is TRUE, the first DBMS_OUTPUT.PUT_LINE is executed.
  7. END IF; and END;: The IF block and the main program block are closed.
Vinish Kapoor
Vinish Kapoor

Vinish Kapoor is a seasoned software development professional and a fervent enthusiast of artificial intelligence (AI). His impressive career spans over 25+ years, marked by a relentless pursuit of innovation and excellence in the field of information technology. As an Oracle ACE, Vinish has distinguished himself as a leading expert in Oracle technologies, a title awarded to individuals who have demonstrated their deep commitment, leadership, and expertise in the Oracle community.

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments