| Nicolas Neuss 2006-01-11, 7:55 am |
| Peter Lacey <lacey@mts.net> writes:
> Elegance is in the eye of the beholder, to a certain extent. But for
> the practical programmer/designer, a sort of "Ockham's Razor" approach
> should be applied: if there is more than one solution/method to solve
> the problem, then the simplest one is the one to use. As you say, the
> other solution is quite easy.
>
> I stand to be corrected on this, of course, but AFAIK recursion (in the
> sense of a program or a paragraph calling itself) is rarely necessary.
>
> PL
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.
|