📜  在 PL/SQL 中检查数字是否为回文

📅  最后修改于: 2022-05-13 01:57:13.081000             🧑  作者: Mango

在 PL/SQL 中检查数字是否为回文

给定一个整数,编写一个函数,如果给定的数字是回文,则返回 true,否则返回 false。例如,12321 是回文,但 1451 不是回文。

让给定的数字为 num。解决这个问题的一个简单方法是先将 num 的数字反转,然后将 num 的反转与 num 进行比较。如果两者相同,则返回真,否则返回假。

例子:

Input : 12221
Output : true

Input : 12345
Output : false

以下是所需的实现:

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

输出:

true