Factorial in Ada

Published on 10 May 2026 (Updated: 10 May 2026)

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.

Current Solution

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.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.