How to find the factorial using function in sql plus

Find the factorial number using function in sql


                               Here we using the software is  SQL*Plus: Release 10.2.0.3.0 - Production.Factorial are product to multiples all whole numbers from 1 to n (eg factorial of 5 then 5*4*3*2*1=120).The function of FUNCTION fact(x in out number,y in number) used to give a value for the argument when calling the function and  return value to the function.




PROGRAM:-

 

set serveroutput on;
DECLARE
a number:=1;
c number;
i number(3);
n number;
--function declaration
FUNCTION fact(x in out number,y in number)
return number is
BEGIN
dbms_output.put_line('Enter the factorial number');
n:=&n;
if n>0 then
for i in 1..n loop

x:=x*i;

end loop;
end if;
return x;

end;
BEGIN 
       --calling a function
a:=fact(a,i);
dbms_output.put_line('The factorial is '||a);
end;
/

In t this program, three variables are declared a, c, i,n. The function name is fact and its argument are x and y and return number is also x. 'n=&n' means we can enter the number of factorials. If(n>0) means give the number greater than zero. For loop is allow to 1 to n values repeat the statements when the condition is true. 'x=x*i' is the equation of factorial.
                 
we can call the function 'a: =fact (a, i)' and value stored to 'a' 
variable then display the factorial.



OUTPUT:-









Post a Comment

0 Comments