The Modula-2 Programming Language:

Factorial Program


Click below to go directly to a specific section:
Description | Source Code | Sample Run | Program Notes

Description

This program contains a function procedure to compute N!. It illustrates the use of modules, procedures in modules, looping, comparison testing, and mathematical operations.

Source Code


MODULE Factorial;
FROM InOut IMPORT WriteCard, WriteLn;

PROCEDURE Fact(n:CARDINAL):CARDINAL;

VAR 
	nfact: CARDINAL;

BEGIN
	IF n > 8 THEN RETURN 0 
	END;
	
	nfact:=1;
	FOR n:=n TO 1 BY -1 DO
	    nfact:=nfact*n
	END;

	RETURN nfact;

END Fact;


VAR
	i:CARDINAL;
	
BEGIN
   FOR i:=0 TO 8 DO 
	WriteCard(i,3);
	WriteCard(Fact(i),12);
	WriteLn
   END
END Factorial.	
	
	


Sample Run


   0		1
   1            1
   2            2
   3            6
   4            24
   5            120
   6		720
   7		5040
   8		40320
      


Program Notes

This program was written with the help of A Guide to Modula-2 by Kaare Christian. It was compiled, tested, and run on Modula-2 compiler version 2.0a
[Back] [Home]

Last modified: 06:29 PM on 11/20/1996