/* fact.pli: Preprocessor program to compute factorials              */
/*           Original by Douglas Wells.                              */
/* This program computes factorials up to 8!, where the limit is     */
/* imposed by the definition of FIXED variables as FIXED DECIMAL(5). */
/* The output of the program is a series if PUT statements.          */

fact: proc options(main);

  dcl	sysprint	file stream output;
 
% dcl	xfact		ENTRY ;
% dcl	FACTBOUND	FIXED ;
% dcl	(i,j)		FIXED ;

% FACTBOUND = 8 ;

	% do i = 1 to FACTBOUND ;
		% j = xfact (i) ;
		put skip edit ("Factorial (", i, ") = ", j)
		  (a, f(2), a, f(20));
		% NOTE ('Factorial (' || i || ') = ' || j, 0) ;
	% end ;
	put skip;

	return;

  % xfact: proc (num) returns (FIXED) ;
     dcl	num	    FIXED ;
     dcl	x	    FIXED ;
     dcl	prod	FIXED ;
     dcl    i       FIXED ;

	x = num ;
	prod = 1 ;

    do i=1;
      if x <= 1
	  then 	return (prod) ;
	  else do;
		prod = x * prod ;
		x = x - 1 ;
	    end ;
	end; /* do while */
  % end xfact ;

end fact;

