The modified Example Program:
! MExample Program 7-3
! Calculate the value of a factorial number.
! Display values for n = 1 to 10, use color and sound to distinguish between
! different types of errors and the result of valid calculations.
! Input data -1,3.3,1,2,3,4,5,6,7,8,9,10 from a data statement
! in a do loop to test the behaviour of the modified program.
! Run do trace, slow (Number) to watch how the program works.
DECLARE FUNCTION Factorial
!INPUT prompt "Enter number: ": Number
DO while more data
READ Number
IF Number < 0 then
SET COLOR "red"
SOUND 500,.3
PRINT "The number must be positive or zero."
ELSEIF Number <> Int(Number) then
SET COLOR "green"
SOUND 300,.3
PRINT "The number must be an integer."
ELSE
SOUND 1000,0.1
SET COLOR "blue" ! blue for a valid factorial calculation
PRINT "The factorial value of"; Number;
PRINT "is"; Factorial(Number)
END IF
LOOP
DATA -1,3.3,1,2,3,4,5,6,7,8,9,10
END
FUNCTION Factorial (X)
! Calculate the factorial value of parameter X.
LET Result = 1 ! initialize the variable Result
FOR I = 1 to X
LET Result = Result * I
NEXT I
LET Factorial = Result
END FUNCTION
The output of the modified program:
The number must be positive or zero.
The number must be an integer.
The factorial value of 1 is 1
The factorial value of 2 is 2
The factorial value of 3 is 6
The factorial value of 4 is 24
The factorial value of 5 is 120
The factorial value of 6 is 720
The factorial value of 7 is 5040
The factorial value of 8 is 40320
The factorial value of 9 is 362880
The factorial value of 10 is 3628800