How to find the factorial of a number in PLSQL (Update)

Find the factorial of a number in PLSQL

Factorial of a number in PLSQL is defined as the specified number and it multiplies with the previous numbers till the end without multiplies with zero. Using PLSQL we can write an easy program using PLSQL. Let us see how we can write a program for that.

factorial of a number in plsql

In the above image, I have captured examples of up to 10 numbers. Using this way you can calculate it for any given number.

Here let us see how to write a program for this.

Given a number, your task is to print the factorial of a number using PLSQL.

Examples:

Consider that the given number is four. The factorial of this number would be 24.

Input  : 4
Output : 24

Explanation:

4! = 4 * 3 * 2 * 1 = 24

Input  : 3
Output : 6

Basic structure of PLSQL block

declare
-- We have to declare all the variables here

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

end -- for end block
The program of factorial of a number in PLSQL is given below:

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 3)

6

Conclusion

Factorial, in mathematics, the value of all positive integers is less than or equal to a given positive integer and denoted by that integer and an exclamation point.

Leave a comment