kolibrios/programs/develop/oberon07/Samples/Windows/Console/fact.ob07

59 lines
888 B
Plaintext
Raw Normal View History

(*
adapted to Oberon-07 by 0CodErr, KolibriOS team
*)
(*
* Written by Andrew Cadach
*
* Recursive (extremely uneficient:-) implementation of factorial
*
* n * (n-1)!, n <> 0
* By definition, n! =
* 1, n = 0
*
*)
MODULE fact;
IMPORT In, Out, Console;
CONST
MAX_INTEGER = ROR(-2, 1);
VAR
i, r: INTEGER;
PROCEDURE f (n: INTEGER): INTEGER;
VAR
Res: INTEGER;
BEGIN
IF n = 0 THEN
Res := 1
ELSE
Res := n * f (n - 1)
END
RETURN Res
END f;
BEGIN
Console.open;
i := 0;
REPEAT
r := f(i);
Out.String ("The factorial of ");
Out.Int (i, 2);
Out.String (" is ");
Out.Int (r, 0);
Out.Ln;
INC(i)
UNTIL r >= MAX_INTEGER DIV i;
In.Ln;
Console.exit(TRUE)
END fact.