Saturday 7 November 2015

PL/SQL factorial program

Write a program in PL/SQL to print the factorial of a number.



In this post I will explain how to get the factorial of any given number. For that first you need to know what is the procedure to find the factorial of a number.
To get the factorial of any given number we have to multiply that number to every number less than that number till 1.
Suppose we have to find the factorial of 5. Then,
factorial(5)=5*4*3*2*1
                  =120


Program Code:

SQL> set serveroutput on
SQL> declare
  2  i number(5);
  3  j number (5);
  4  f number(10);
  5  begin
  6  i:=&i;
  7  f:=1;
  8  for j in 1..i
  9  loop
 10  f:=f*j;
 11  end loop;
 12  dbms_output.put_line(f);
 13  end;
 14  /

Thanks

No comments:

Post a Comment