home *** CD-ROM | disk | FTP | other *** search
- -- Chapter 18 - Program 4
- with Text_IO;
- use Text_IO;
-
- procedure FuncRecr is
-
- package Int_IO is new Text_IO.Integer_IO(INTEGER);
- use Int_IO;
-
- START : constant := -2;
- Result : INTEGER;
- Data_Value : INTEGER;
-
- function Factorial_Possible(Number : INTEGER) return BOOLEAN;
-
- function Factorial(Number : INTEGER) return INTEGER is
- begin
- if not Factorial_Possible(Number) then
- Put("Factorial not possible for");
- Put(Number);
- New_Line;
- return 0;
- end if;
- if Number = 0 then
- return 1;
- elsif Number = 1 then
- return 1;
- else
- return Factorial(Number - 1) * Number;
- end if;
- end Factorial;
-
- function Factorial_Possible(Number : INTEGER) return BOOLEAN is
- begin
- if Number >= 0 then
- return TRUE;
- else
- return FALSE;
- end if;
- end Factorial_Possible;
-
- begin
- Put("Factorial program");
- New_Line(2);
-
- for Number_To_Try in START..5 loop
- Put(Number_To_Try);
- if Factorial_Possible(Number_To_Try) then
- Result := Factorial(Number_To_Try);
- Put(" is legal to factorialize and the result is");
- Put(Result);
- else
- Put(" is not legal to factorialize.");
- end if;
- New_Line;
- end loop;
-
- New_Line;
- Data_Value := 4;
- Result := Factorial(Data_Value + 3); -- Factorial(7)
- Result := Factorial(2*Data_Value - 3); -- Factorial(5)
- Result := Factorial(2 - Data_Value); -- Factorial(-2)
- Result := Factorial(Factorial(3)); -- Factorial(6)
- Result := Factorial(4); -- Factorial(4)
- Result := Factorial(0); -- Factorial(0)
-
- end FuncRecr;
-
-
-
-
- -- Result of Execution
-
- -- Factorial program
- --
- -- -2 is not legal to factorialize.
- -- -1 is not legal to factorialize.
- -- 0 is legal to factorialize and the result is 1
- -- 1 is legal to factorialize and the result is 1
- -- 2 is legal to factorialize and the result is 2
- -- 3 is legal to factorialize and the result is 6
- -- 4 is legal to factorialize and the result is 24
- -- 5 is legal to factorialize and the result is 120
- --
- -- Factorial not possible for -2
-
-