PL/SQL program to check for palindrome number

In this program, you will learn to check whether the number entered by the user is palindrome or not.

when the reversed number is equal to the original number then the number is a palindrome. for example, 101, 121, 1234321, etc are palindrome numbers.


declare
n number;
copy number;
rev number := 0;
begin
/* take input from user */
n:=&n;
copy:=n;
while(copy>0)
loop
  /*It will find the reverse of the number entered by the user.*/
  rev := rev * 10 + mod(n,10);
  copy := copy / 10;
end loop; 
/* check if reverse number is equal to original number */
if (n = rev)
  then
    dbms_output.put_line(n||' is palindrome number');
  else
    dbms_output.put_line(n||' is not palindrome number');
  end if; 
end;
/
output
Enter value for n: 123
old   7: n:=&n;
new   7: n:=123;
123 is not palindrome number

Logic for palindrome number

  • take the number from the user and store it in variable n.
  • find the reverse of the user entered number i.e n and store it in variable rev.
  • now compare the n and rev (reverse number). if both are the same then the number is a palindrome.