• 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. Oracle Database Storage Architecture – Overview
  2. How to find daily and hourly archive log generation in Oracle Database
  3. Oracle Scheduler 12c New Features and Enhancements
  4. How To Change SYS user password for oracle database instance
  5. Where to find ASMLib / oracleasm RPMs for CentOS/RHEL, SUSE, OEL
  6. How To Run ggsci In “silent” Mode
  7. RMAN Pluggable Database Backup and Recovery in a Multitenant Environment
  8. How to Drop/Truncate Multiple Partitions in Oracle 12C
  9. How to Switch to a New Undo Tablespace in Oracle Database
  10. How to Disable AUTOEXTEND Mode on a datafile in Oracle Database

You May Also Like

Primary Sidebar

Recent Posts

  • grpck command – Remove corrupt or duplicate entries in the /etc/group and /etc/gshadow files.
  • xxd command – Expressed in hexadecimal form
  • sesearch: command not found
  • macof: command not found

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright