• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

PL/SQL: Factorial Program

by admin

Factorial: The product of an integer and all the integers below it; e.g. factorial four ( 4! ) is equal to:

4*3*2*1=24

To put it in an Oracle PL/SQL code, I would create a function that will take the input of an integer whose factorial is to be calculated and the return value would be the factorial of the given input.

CREATE OR REPLACE FUNCTION GET_FACTORIAL (P_INT NUMBER)
RETURN NUMBER
IS
   P_FACT PLS_INTEGER := 1;
BEGIN
   FOR I IN 1 .. P_INT
   LOOP
     P_FACT := P_FACT * I; 
   END LOOP; 
RETURN P_FACT;
END;
/

The same can now be executed from a select statement as follows:

SELECT GET_FACTORIAL(6) FROM DUAL;

Output: 720

Filed Under: oracle

Some more articles you might also be interested in …

  1. Why Can I Login AS SYSDBA With Any Username and Password
  2. Basics of client connectivity in Oracle Data Guard configuration
  3. What is the difference between PRIMARY Key and UNIQUE Key
  4. How to Connect without password on Command line when using EZCONNECT
  5. PL/SQL: Palindrome Program
  6. How to split BCV and open oracle ASM database
  7. How to Delete ASM Disk on Multipath Device in CentOS/RHEL
  8. Managing Oracle Database Backup with RMAN (Examples included)
  9. How to Perform Manual Archiving in Oracle Database
  10. How to trace asmcmd command on UNIX/Linux

You May Also Like

Primary Sidebar

Recent Posts

  • aws ec2: CLI for AWS EC2 (Command Examples)
  • aws cur – Create, query, and delete AWS usage report definitions (Command Examples)
  • aws configure – Manage configuration for the AWS CLI (Command Examples)
  • aws cognito-idp: Manage Amazon Cognito user pool and its users and groups using the CLI

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright