Build it.
[AROS-Contrib.git] / rexx / progs / fact.r
blob9d9af0277287cbcc91e9ca464ec1c3e4783a4ed1
1 /* recursion test of factorial */
2 /* note: this implementation of rexx uses two kinds of
3 of numbers integers (C long (4-bytes) numbers from -2E9 upto 2E9),
4 and real (C double precision numbers). It's better
5 to use a real number ie 45.0 instead of 45 for large
6 factorials since the later will result to a factorial using
7 integer arithmetic resulting in a crazy result
8 (15 is the largest integer factorial that can be calculated
9 with out any error )
11 if arg() ^= 1 then do
12 say 'Enter a number'
13 pull num
14 end; else do
15 num = arg(1)
16 end
18 if datatype(num) ^= 'NUM' | num < 0 then do
19 say 'Invalid number "'num'"'
20 exit 2
21 end
23 /* you can even translate the number to real with the following instr */
24 /* num = num + 0.0 /* so from now on num will be treated as real */ */
26 say num'! = 'fact(num)
27 exit
29 fact: procedure
30 if arg(1)<=0 then return 1
31 return fact(arg(1)-1)*arg(1)