Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Factorial of a number in PL/SQL

  • Difficulty Level : Basic
  • Last Updated : 10 May, 2018


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.

Attention reader! All those who say programming isn't for kids, just haven't met the right mentors yet. Join the  Demo Class for First Step to Coding Coursespecifically designed for students of class 8 to 12. 

The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.

Examples:

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

Below is the required implementation:




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

Output :

factorial of 6 is 720.
My Personal Notes arrow_drop_up
Recommended Articles
Page :