Factorial in Pascal
Example for versions
Free Pascal 2.0.4,
Free Pascal 2.2.0,
Turbo Pascal 1.0,
Turbo Pascal 2.0,
Turbo Pascal 3.0,
Turbo Pascal 4.0,
Turbo Pascal 5.0,
Turbo Pascal 5.5,
Turbo Pascal 6.0,
Turbo Pascal 7.0,
gpc 20070904
This example is exactly the same as main recursive example for Pascal implementations, except for that it uses real
data type to store factorial values. Command writeln(f:-1:0)
outputs the floating point number f
with 0 digits after decimal point and left-justifies it.
program factorial;
function fact(n: integer): real;
begin
if (n = 0) then
fact := 1
else
fact := n * fact(n - 1);
end;
var
n: integer;
begin
for n := 0 to 16 do
writeln(n, '! = ', fact(n):-1:0);
end.
Comments
]]>blog comments powered by Disqus
]]>