home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 24 DOS / 24-DOS.zip / dosrx10b.zip / FACT.R < prev    next >
Text File  |  1993-01-03  |  906b  |  31 lines

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