# Write a Program to Factorial of a Number Using Perl

INTRODUCTION

In the following Perl program, we are going to find the factorial of a number. In mathematics, the factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,
5!=5*4*3*2*1=120.
The value of O! is 1.
The factorial operation is used in many areas of mathematics, especially in combinatorics, algebra and mathematical analysis. It's a most basic event is the fact that there are n! ways to arrange n distinct objects into a sequence. This fact was known atlist as well as the 12th century, to Indian scholars.
The notation n! was introduced by Christian Kramp in 1808.
by definition,
The factorial function is defined by the product
n!={1 if n=0,(n-1)!*n if n>0

ALGORITHM
Step 1: START
Step 2: READ THE NUMBER N
Step 3: [INITIALIZE]I=1, Fact=1
Step 4: IF N>=0, THEN GO TO STEP 6
Step 5: PRINT WRONG INPUT
Step 6: REPEAT STEP 6 TO 8 WHILE I<=N
Step 7: Fact=Fact*|
Step 8: 1=1+1 (INCREMENT I BY ONE)
Step 9: PRINT Fact Step
10: STOP

FLOW CHART

PROGRAM TO FACTORIAL OF A NUMBER

#!/usr/bin/perl/
my(\$n, \$fact); #!Declaration of variables used in the program
chomp(\$n); #!To get the actual value of \$n
Sn=int(\$n); #!Get and store the integer value of \$n
print"\n Enter A Number"; #!Print statement when program is compiled
\$n=<stdin>; #!To get value of \$n from user
\$fact=1; #!\$fact is initialized with the value of 1
if(\$n>=0) #!Checking the value of \$n is greater or equal to 0 or not
{ #!lf condition satisfied
for(\$i=1;\$i<=\$n;\$i++) #!A loop to count the factorial until it equals to \$n
{
\$fact=\$fact *\$i; #!The multiply of \$fact & \$i will be stored on \$fact
}
print"\n Factorial value of the number is \$fact"; #!Printing the value of \$fact
}
else #!lf condition does not satisfied
{
print"\n Wrong Input"; #!Printing that the input is wrong
}
<>; #!The program is resumed until any character press from the keyboard

DESCRIPTION OF VARIABLES USED IN THE PROGRAM

Variable Name Variable Type Used To
\$n Schalar Store the input from the user to count
\$fact Schalar To store the final value of factorial of the number
\$i Schalar Count the loop for the factorial of the given number

INPUT:
We have to check the program using the following input:
1. 1st case by giving input as -4
2. 2nd case by giving input as 6
3. 3rd case by giving input as 13

OUTPUT:
Here is the output generated by the program
1st case:
Enter A Number -4
Wrong Input

2nd case:
Enter A Number 6
Factorial value of the no is 726

3rd case:
Enter A Number 13
Factorial value of the no is 6229029800

## DISCUSSION

If efficiency is not a concern, computing factorial of a number is trivial form, an algorithm, point of view, successively convert the number into its factorial form.

In the above program, we used <stdin> for standard input of \$n to accept and store the number from the user to find the factorial of that given number. We used the chomp function to store the actual value of \$n and avoid the garbage value which is given by the user. We used the int(\$n) to store the integer value (only positive integers not a fractional number or negative numbers) to count its factorial which is given by the user. After that, we used a loop with counter variable şi (initialized \$i=1) which will count the factorial of the given number and after each successful execution of loop as well as increment itself.

The program will also check for correct input whether the input is a positive integer or 0 produce an appropriate result.