home *** CD-ROM | disk | FTP | other *** search
- {---------------------------------------------------------------------
- PROGRAM: FACTORIAL2.PAS
-
- This program is taken from:
-
- Leestma, Sanford and Larry Nyhoff
- Pascal: Programming and Problem Solving
- MacMillan, 1984
- pp. 162 and 192
-
- Its purpose is to examine two ways to calculate factorial. One is
- through a FOR DO loop, the other is through Recursion.
-
- INPUT:
-
- OUTPUT:
-
- VAR:
-
-
- Author: Mike Benedict
- Date Started: 5/27/91
- Latest Revision: 5/27/91
- Version: Turbo Pascal 6.0
-
- -------------------------------------------------------------------}
-
- PROGRAM Factrl2 (Input, Output);
-
- USES
- Crt;
-
- CONST
- MaxM = 7;
- VAR
- Choice,
- M : Integer;
- UserQuits : BOOLEAN;
-
- {------------------------------------}
- { PROCEDURES }
- {------------------------------------}
-
- {-----------------------------}
- { INIT.PROC }
- {-----------------------------}
-
- PROCEDURE Init;
-
- BEGIN
- TextBackground(Blue); { Sets colors }
- TextColor(White);
- Window(0,0,80,25);
- ClrScr;
- END;
-
-
-
- {-----------------------------}
- { .PROC }
- {-----------------------------}
-
- PROCEDURE Null;
-
- BEGIN
- END;
-
- {-----------------------------}
- { FACTFORDO .PROC }
- {-----------------------------}
-
- PROCEDURE FactForDo;
-
- VAR
- k, { index }
- FAC,
- Factorial : INTEGER; { partial factorial }
-
- BEGIN
- ClrScr;
- WriteLn;
- Write(' ':20,' Enter an integer - 0 to ',MaxM,': ');
- ReadLn(M);
- IF M <= MaxM THEN
- BEGIN
- FAC := 1;
- FOR k := 2 to M DO
- FAC := FAC * K;
- Factorial := FAC;
- WriteLn;
- WriteLn(' ':20,' M was entered as: ', M );
- WriteLn;
- WriteLn(' ':20,' M!, or M Factorial = ', Factorial );
- END
- ELSE
- BEGIN
- WriteLn;
- WriteLn('The number you entered is greater than ',MaxM,'. Re-enter. ');
- END; { ELSE }
- WriteLn;
- WriteLn(' Press <ENTER> to return to menu.');
- ReadLn;
- END; {FactForDo}
-
-
-
- {-----------------------------}
- { FACTORIAL.PROC }
- {-----------------------------}
-
- FUNCTION Factorial ( M : Integer ) : Integer;
-
- BEGIN
- IF M = 0 THEN
- Factorial := 1
- ELSE
- Factorial := M * Factorial( M-1 );
- END; { Factorial }
-
-
- {-----------------------------}
- { FACTREC.PROC }
- {-----------------------------}
-
- PROCEDURE FactRec;
-
- BEGIN
- ClrScr;
- WriteLn;
- Write(' ':20,'Enter an integer - 0 to ',MaxM,': ');
- ReadLn(M);
- WriteLn;
- IF M > MaxM THEN
- WriteLn('The number you entered is greater than ',MaxM,'. Re-enter. ')
- ELSE
- BEGIN
- WriteLn;
- WriteLn(' ':20,' M was entered as: ', M );
- M := Factorial( M );
- END; { ELSE }
- WriteLn;
- WriteLn(' ':20,' M!, or M Factorial = ', M );
- WriteLn;
- WriteLn(' Press <ENTER> to return to menu.');
- ReadLn;
- END; {FactRec}
-
-
-
- {------------------------------------}
- { MAIN PROGRAM }
- {------------------------------------}
-
- BEGIN
- Init;
- UserQuits := False;
- REPEAT
- ClrScr;
- WriteLn;
- WriteLn(' ':25, ' Menu For Factorial Methods ' );
- WriteLn;
- WriteLn(' ':20, ' 1. Factorial Using a FOR DO Loop' );
- WriteLn(' ':20, ' 2. Factorial Using Recursion' );
- WriteLn(' ':20, ' 3. Quit ' );
- ReadLn ( Choice );
- CASE Choice OF
- 1 : FactForDo;
- 2 : FactRec;
- 3 : UserQuits := True;
- END;
- UNTIL UserQuits;
- END.
-