# PL/SQL program to find factorial of a number

In this example, you will take the input number from the user and find the factorial of a number in PL/SQL

example
```input: 5
output: 120 ```

factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120

## find factorial using for loop

``````
declare
n number;
fact number := 1;
i number := 1;
c number := 0;
begin
/* take input from the user */
n:=&n;
if(n<0)
then
/* display error when a user entered a negative number */
dbms_output.put_line('factorial of negative number does not exist');
end if;
for i in 1..n
loop
fact := fact * i;
end loop;
dbms_output.put_line('factorial of '||n||' is '||fact);
end;
/
``````
output
```Enter value for n: 5
old   7: n:=&n;
new   7: n:=5;
factorial of 5 is 120```

## Calculate factorial using while loop

This program is the same as above only here we have used a while loop

``````
declare
n number;
fact number := 1;
i number := 1;
c number := 0;
begin
n:=&n;
if(n<0)
then
dbms_output.put_line('factorial of negative number does not exist');
end if;
while(i<=n)
loop
fact := fact * i;
i := i + 1;
end loop;
dbms_output.put_line('factorial of '||n||' is '||fact);
end;
/
``````
output
``` 19  /
Enter value for n: 4
old   7: n:=&n;
new   7: n:=4;
factorial of 4 is 24```