PROGRAM TestFunc;
USES crt;
VAR
     num: longint;
     ans: string;

FUNCTION Factorial(x: longint):longint;
VAR
     temp: longint;
     i:    integer;

BEGIN
     temp:= x;
     CASE temp OF
         0:     Factorial:= 1;
         1:     Factorial:= 1;
         ELSE
	     BEGIN
     		FOR i:= x DOWNTO 2 DO
         		BEGIN
           		   temp:= temp * (x-1);
      		           x:=x-1;
         		END;  {for loop}
     		Factorial:= temp;
                END;  {else clause}
             END; {case statement}
END; {function factorial}

BEGIN
     
     ans:='y';
     WHILE (ans='y') DO
     BEGIN
	clrscr;	
     	Writeln('Enter in an integer');
    	Readln(num);

        IF (num<0) THEN Writeln('ERROR! Must enter in a natural number.')
        ELSE IF (num>16) THEN
          BEGIN
             Writeln('The precision limit on integers in pascal restricts');
             Writeln('the calculation of factorials to 16 and under.');
          END  {Else if}
        ELSE
            BEGIN
                 Writeln(num,'! = ', Factorial(num));
     	         Writeln;
            END; {Else statement}

	Writeln('Try again? "y" or "n" ');
	Readln(ans);
     END;  {while}

END.
