A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Factorial in Ada page! Here, you'll find the source code for this program as well as a description of how the program works.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
procedure Factorial is
procedure Print_Usage is
begin
Put_Line ("Usage: please input a non-negative integer");
end Print_Usage;
function Fact (N : Natural) return Natural is
Result : Natural := 1;
begin
for I in 2 .. N loop
Result := Result * I;
end loop;
return Result;
end Fact;
Max_Allowed : constant Natural := 12; -- Fact (13) > Natural'Last
begin
if Argument_Count /= 1 then
Print_Usage;
return;
end if;
declare
N : Natural;
begin
begin
N := Natural'Value (Argument (1));
exception
when Constraint_Error =>
Print_Usage;
return;
end;
if N > Max_Allowed then
Print_Usage;
return;
end if;
Put_Line (Natural'Image (Fact (N)));
end;
end Factorial;
Factorial in Ada was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.