Open In App

Find the factorial of a number in pl/sql

Last Updated : 06 May, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
Given a number, your task to print the factorial of that number using pl/sql. Examples:
Input : 5
Output : 120
Explanation: 5! = 5 * 4 * 3 * 2 * 1 = 120
Input : 4
Output : 24
Basic structure of pl/sql block
declare
-- declare all the variables

begin  -- for start block
-- make a program here

end -- for end block
The program of factorial of a number in pl/sql is given below: SQL
declare 
-- it gives the final answer after computation
fac number :=1;   

-- given number n
-- taking input from user
n number := &1;   

-- start block
begin         

-- start while loop    
while n > 0 loop  

-- multiple with n and decrease n's value
fac:=n*fac;        
n:=n-1;           
end loop;         
-- end loop

-- print result of fac
dbms_output.put_line(fac);  

-- end the begin block
end;              
Output:(if given input as 5)
120

Next Article
Article Tags :
Practice Tags :

Similar Reads