MODULE fact2; (*--------------------------------------------------------- File name: fact2.pm Author: AG, MM Date: 5/19/05 Problem: This program calculates factorial sequentially. A PROCEDURE is defined which does the operations, main calls the procedure. Procedure uses a WHILE loop to calculate factorial. No configuration is defined, everything is calculated trought the controller Example of Execution: Enter N?: 5 N! = 120 Cycles: N multiplications + N additions. *) (*---------------------------------------------------------- VARIABLES ----------------------------------------------------------*) VAR N, F: INTEGER; (*---------------------------------------------------------- PROCEDURES ----------------------------------------------------------*) PROCEDURE Fact(N: INTEGER); (*---------------------------------------------------------- PROCEDURE VARIABLES ----------------------------------------------------------*) VAR K: INTEGER; (*---------------------------------------------------------- PROCEDURE MAIN ----------------------------------------------------------*) BEGIN F:=1; K:=1; WHILE K <= N DO F:= F*K; K:= K + 1; END; (*FOR *) END Fact; (* Result is in Global variable F *) (*---------------------------------------------------------- MAIN ----------------------------------------------------------*) BEGIN (*Reading *) WriteString("Enter N?: "); ReadInt(N); (* Read N *) (* call function *) Fact(N); (*printing*) WriteString(" N! = " ); WriteInt(F, 2); END fact2.