# Factorial (Pascal)

### From LiteratePrograms

In mathematics, the factorial of a positive integer n is the product of all positive integers less than or equal to n. This is written as n! and pronounced "n factorial", or colloquially "n shriek", "n bang" or "n crit". The notation n! was introduced by Christian Kramp in 1808.

## Definition

The factorial function is formally defined by

For example,

The above definition incorporates the convention that

as an instance of the convention that the product of no numbers at all is 1. This fact for factorials is useful, because

- the recursive relation works for
*n*= 0; - this definition makes many identities in combinatorics valid for zero sizes.

## Computation

The numeric value of **n!** can be calculated by repeated multiplication if **n** is not too large. That is basically what pocket calculators do. The largest factorial that most calculators can handle is 69!, because 70! > 10^{100}. In practice, most software applications use only small factorials which can be computed by direct multiplication or table lookup. Larger values are often approximated in terms of floating-point estimates of the Gamma function, usually with Stirling's formula.

Here we'll create a very simple Pascal program to compute smaller factorials . It uses a for-to-do loop.

<<Factorial.pas>>=programFactorial; {The program computes 0! through 170!}vari, x : integer; {''i'' here stands for iteration} fact : real; {Usually factorials are so large that we can't use integer here}beginwriteln ('Enter the value of the factorial'); readln (x); fact:=1; {As we can't multiply by 0, our first number should be 1}fori:=1toxdo{Here is our loop} fact :=fact*i; writeln(x,'! is ',fact); readln; {The semicolon is not obligatory here}end.

Download code |