Open In App

Check if a number is Palindrome in PL/SQL

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given an integer, write a function that returns true if the given number is palindrome, else false. For example, 12321 is palindrome, but 1451 is not palindrome.

Let the given number be num. A simple method for this problem is to first reverse digits of num, then compare the reverse of num with num. If both are same, then return true, else false.

Examples:

Input : 12221
Output : true

Input : 12345
Output : false

Below is the required implementation:




declare
  
-- declare variable n, m, temp 
-- and temp of datatype number
    n number;
    m number;
    temp number:=0;
    rem number;
   
begin
    n:=5432112345;
    m:=n;
      
    -- while loop with condition till n>0
    while n>0
    loop
        rem:=mod(n,10);
        temp:=(temp*10)+rem;
        n:=trunc(n/10);
    end loop; -- end of while loop here
      
    if m = temp
    then
        dbms_output.put_line('true');
    else
        dbms_output.put_line('false');
    end if;
end;
/
  
-- Program End


Output:

true

Last Updated : 13 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads