{ ******************************************************************* }
{  Program to calculate the factorial of a number N, illustrating the }
{  use of a recursive function, i.e. one which calls itself.          }
{  Implements the  inductive definition of factorial:                 }
{   factorial 1 = 1                                                   }
{   factorial N = N.factorial(N-1), N > 1                             }
{ ******************************************************************* }

program recursive_factorial( input, output );
var 
  N, count, fact : integer;

function factorial( N: integer ): integer;
begin
  if N <= 1
  then factorial := 1
  else factorial := N * factorial( N-1 )
end; { fact }

begin
  { Get N from the user }
  write( 'N = ' ); readln(N);

  { Negative N is not allowed }
  if N < 0
  then 
    writeln( 'Cannot take the factorial of a negative number' )
  else
    begin
      fact := factorial(N);
      writeln( 'Factorial ', N:1, ' = ', fact:1 );
    end; { N >= 0 }
end.
