| William M. Klein 2006-01-11, 6:55 pm |
| So (based on your version) what about :
> identification division.
> program-id. FactorialPgm.
> data division.
> working-storage section.
> 01 Working-Fields
05 result pic 9(8).
> 05 Num pic 9(8). *> Using "N" as a data-name is not
> considered good COBOL.
05 Num-Redefined Redefines Num Pic X(8).
> procedure division.
> main-line.
> display "Enter a positive number: "
with no advancing *> with no advancing MIGHT produce
undesired results
> accept n
If Num-Redefined numeric *> some accept/display may not
prevent bad input for PIC N fields
Compute Result = Function Factorial (Num)
On Size Error
Display "Number larger than 8 digits"
Not On Size Error
> display "Factorial(" Num ")= " result
End-Compute
Else
Display "Unable to determine FACTORIAL for non-numeric
input"
End-If
> Stop Run.
--
Bill Klein
wmklein <at> ix.netcom.com
"Nicolas Neuss" <firstname.lastname@iwr.uni-heidelberg.de> wrote in message
news:87d5izqdto.fsf@ortler.iwr.uni-heidelberg.de...
> Peter Lacey <lacey@mts.net> writes:
>
>
> I do not necessarily want a recursive solution. I want a solution which
> fits the language best. At the moment, combining the suggestions obtained
> here, I favor the following:
>
> identification division.
> program-id. factorial.
> environment division.
> data division.
> working-storage section.
> 77 result pic 9(8).
> 77 n pic 9(8).
> 77 i pic 9(8).
> procedure division.
> main-line.
> display "Enter a positive number: " with no advancing
> accept n
> move 1 to result.
> perform varying i from 1 by 1 until i>n
> multiply result by i giving result
> end-perform
> display "Factorial(" n ")= " result
> stop run.
>
> This version works for my implementation, and is acceptable for Cobol
> programmers, I hope. But I am open to further improvements...
>
> Thank you very much,
>
> Nicolas.
|