! PROGRAM P46 ! ! FACTORIAL N ! IMPLICIT NONE INTEGER :: I,N,FACT ! ! PRINT *, 'This is Program >> P46 - Factorial program' ! ! ! Tell program where data for READ * is coming from OPEN(UNIT=5, FILE='P46.DAT') ! UNIT=5 is the default input ! PRINT 1000 1000 FORMAT(' Factorial Program'/) L1: DO READ *,N IF(N < 0) STOP ! or EXIT in this case ! FACT=1 L2: DO I=2,N FACT=FACT*I END DO L2 ! PRINT 10,N,FACT 10 FORMAT(' Factorial',I3,' =',I8) ! END DO L1 STOP END PROGRAM P46DATA:
5 10 0 -9OUTPUT:
+--------------------------------------------------+ | 32-bit Power for Lahey Computer Systems | | Phar Lap's 386|DOS-Extender(tm) Version 7.0 | | Copyright (C) 1986-94 Phar Lap Software, Inc. | | Available Memory = 14880 Kb | +--------------------------------------------------+ This is Program >> P46 - Factorial program Factorial Program Factorial 5 = 120 Factorial 10 = 3628800 Factorial 0 = 1
PROGRAM p46 (input, output); { Factorial n } VAR i, n : INTEGER; fact : REAL; BEGIN writeln (^l); writeln ( 'Factorial Program' ); readln (n); WHILE ( n >= 0 ) DO BEGIN fact := 1; FOR i := 2 to n DO fact := fact * i; writeln ( ' factorial ', n:3, ' =', fact:8:0 ); readln ( n ) END { end while } END.
Last modified: 21/07/97