Factorial of a number in PL/SQL

Prerequisite PL/SQL introduction
In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements.
In declare part, we declare variables and between begin and end part, we perform the operations.

Here, first, we take three variables num, fact, and temp and assign the value in num variable (i.e which number factorial we want).
And then after we assign fact variable to 1.


Input : 4
Output : 24
Input : 6
Output : 720

Below is the required implementation:





-- declare variable num , fact
-- and temp of datatype number
 num number := 6;             
 fact number := 1;            
 temp number;        
temp :=num;
-- here we check condition 
-- with the help of while loop
while( temp>0 )             
fact := fact*temp;
temp := temp-1;
end loop;
dbms_output.put_line('factorial of '|| num || ' is ' || fact);
-- Program End


Output :

factorial of 6 is 720.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.