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:
- 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:
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.
WHILELoop: We use this loop to reverse the number.MODandTRUNC: We use these math functions to get the last digit (MOD) and then remove it (TRUNC).IF...THEN...ELSELogic: 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
DECLAREsection: We create our variables.v_numis set to121.v_original_numis not set yet.BEGINsection: The logic starts.v_original_num := v_num;: This is a critical step. We store a copy of121inv_original_num. We must do this because theWHILEloop will destroy the value inv_num(it will end up as0).WHILE v_num > 0 LOOP: The loop begins. It will run as long asv_numis 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
- Loop 1:
END LOOP;: The loop stops becausev_numis now0.IF v_original_num = v_reverse THEN: This is the final check.- The program compares
v_original_num(which is still121) tov_reverse(which is now121). - Since
121 = 121isTRUE, the firstDBMS_OUTPUT.PUT_LINEis executed.
- The program compares
END IF;andEND;: TheIFblock and the main program block are closed.
