先决条件——PL/SQL 介绍
在 PL/SQL 代码中,命令组被安排在一个块中。块组相关的声明或语句。
在声明部分,我们声明变量,在开始和结束部分之间,我们执行操作。
在这里,首先,我们取三个变量 num、fact 和 temp,并在 num 变量中赋值(即我们想要哪个数字阶乘)。
然后在我们将事实变量赋值为 1 之后。
例子:
Input : 4
Output : 24
Input : 6
Output : 720
以下是所需的实现:
declare
-- declare variable num , fact
-- and temp of datatype number
num number := 6;
fact number := 1;
temp number;
begin
temp :=num;
-- here we check condition
-- with the help of while loop
while( temp>0 )
loop
fact := fact*temp;
temp := temp-1;
end loop;
dbms_output.put_line('factorial of '|| num || ' is ' || fact);
end;
-- Program End
输出 :
factorial of 6 is 720.