home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog4 / funcrecr.ada < prev    next >
Encoding:
Text File  |  1991-07-01  |  2.3 KB  |  87 lines

  1.                                        -- Chapter 18 - Program 4
  2. with Text_IO;
  3. use Text_IO;
  4.  
  5. procedure FuncRecr is
  6.  
  7.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  8.    use Int_IO;
  9.  
  10.    START      : constant := -2;
  11.    Result     : INTEGER;
  12.    Data_Value : INTEGER;
  13.  
  14.    function Factorial_Possible(Number : INTEGER) return BOOLEAN;
  15.  
  16.    function Factorial(Number : INTEGER) return INTEGER is
  17.    begin
  18.       if not Factorial_Possible(Number) then
  19.          Put("Factorial not possible for");
  20.          Put(Number);
  21.          New_Line;
  22.          return 0;
  23.       end if;
  24.       if Number = 0 then
  25.          return 1;
  26.       elsif Number = 1 then
  27.          return 1;
  28.       else
  29.          return Factorial(Number - 1) * Number;
  30.       end if;
  31.    end Factorial;
  32.  
  33.    function Factorial_Possible(Number : INTEGER) return BOOLEAN is
  34.    begin
  35.       if Number >= 0 then
  36.          return TRUE;
  37.       else
  38.          return FALSE;
  39.       end if;
  40.    end Factorial_Possible;
  41.  
  42. begin
  43.    Put("Factorial program");
  44.    New_Line(2);
  45.  
  46.    for Number_To_Try in START..5 loop
  47.       Put(Number_To_Try);
  48.       if Factorial_Possible(Number_To_Try) then
  49.          Result := Factorial(Number_To_Try);
  50.          Put(" is legal to factorialize and the result is");
  51.          Put(Result);
  52.       else
  53.          Put(" is not legal to factorialize.");
  54.       end if;
  55.       New_Line;
  56.    end loop;
  57.  
  58.    New_Line;
  59.    Data_Value := 4;
  60.    Result := Factorial(Data_Value + 3);       -- Factorial(7)
  61.    Result := Factorial(2*Data_Value - 3);     -- Factorial(5)
  62.    Result := Factorial(2 - Data_Value);       -- Factorial(-2)
  63.    Result := Factorial(Factorial(3));         -- Factorial(6)
  64.    Result := Factorial(4);                    -- Factorial(4)
  65.    Result := Factorial(0);                    -- Factorial(0)
  66.  
  67. end FuncRecr;
  68.  
  69.  
  70.  
  71.  
  72. -- Result of Execution
  73.  
  74. -- Factorial program
  75. --
  76. --     -2 is not legal to factorialize.
  77. --     -1 is not legal to factorialize.
  78. --      0 is legal to factorialize and the result is     1
  79. --      1 is legal to factorialize and the result is     1
  80. --      2 is legal to factorialize and the result is     2
  81. --      3 is legal to factorialize and the result is     6
  82. --      4 is legal to factorialize and the result is    24
  83. --      5 is legal to factorialize and the result is   120
  84. --
  85. -- Factorial not possible for    -2
  86.  
  87.