先决条件——PL/SQL 介绍
在 PL/SQL 代码中,命令组被安排在一个块中。块组相关的声明或语句。
在声明部分,我们声明变量,在开始和结束部分之间,我们执行操作。
解释:
考虑这个例子,输入 = 12345。
第 1 步:mod(12345,10) = 5
转速:= 0*10 + 5 = 5
数量 = 楼层 (12345/10) = 1234
第 2 步:mod(1234,10) = 4
转速:= 5*10 + 4 = 54
数量 = 楼层 (1234/10) = 123
第 3 步:mod(123,10) = 3
转速:= 54*10 + 3 = 543
数量 = 楼层 (123/10) = 12
第 4 步:mod(12,10) = 2
转速:= 543*10 + 2 = 5432
数量 = 楼层 (12/10) = 1
第 5 步:mod(1,10) = 1
转速:= 5432*10 + 1 = 54321
数量 = 楼层(1/10)= 0
在第 5 步中,num =0 不满足 while 条件,循环终止。
转速 = 54321
更多例子:
Input : 123456
Output :654321
Input :87459
Output :95478
以下是所需的实现:
SET SERVEROUTPUT ON;
DECLARE
-- declare a number 'num' for reading actual input
-- declare another number 'rev' that would be reverse of num
num NUMBER;
rev NUMBER;
BEGIN
-- & is used to read input from keyboard
num:=#
-- initialize rev to 0
rev:=0;
-- the loop runs until num is greater than 0
WHILE num>0 LOOP
-- mod function is used to find the modulus/ remainder of num when divided by 10
rev:=(rev*10) + mod(num,10);
-- floor function is used to obtain a result which is an integer
num:=floor(num/10);
END LOOP;
DBMS_OUTPUT.PUT_LINE('Reverse of the number is: ' || rev);
END;
/
-- Program End
输出:
Enter value for num : 157439
Reverse of the number is: 934751