Source of FactorialCalculator.java


  1: // Fig. 15.3: FactorialCalculator.java
  2: // Recursive factorial method.
  3: 
  4: public class FactorialCalculator
  5: {
  6:    // recursive declaration of method factorial
  7:    public long factorial( long number )
  8:    {
  9:       if ( number <= 1 ) // test for base case
 10:          return 1; // base cases: 0! = 1 and 1! = 1
 11:       else // recursion step
 12:          return number * factorial( number - 1 );
 13:    } // end method factorial
 14: 
 15:    // output factorials for values 0-10
 16:    public void displayFactorials()
 17:    {
 18:       // calculate the factorials of 0 through 10
 19:       for ( int counter = 0; counter <= 10; counter++ )
 20:          System.out.printf( "%d! = %d\n", counter, factorial( counter ) );
 21:    } // end method displayFactorials
 22: } // end class FactorialCalculator
 23: 
 24: /*************************************************************************
 25: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 26: * Pearson Education, Inc. All Rights Reserved.                           *
 27: *                                                                        *
 28: * DISCLAIMER: The authors and publisher of this book have used their     *
 29: * best efforts in preparing the book. These efforts include the          *
 30: * development, research, and testing of the theories and programs        *
 31: * to determine their effectiveness. The authors and publisher make       *
 32: * no warranty of any kind, expressed or implied, with regard to these    *
 33: * programs or to the documentation contained in these books. The authors *
 34: * and publisher shall not be liable in any event for incidental or       *
 35: * consequential damages in connection with, or arising out of, the       *
 36: * furnishing, performance, or use of these programs.                     *
 37: *************************************************************************/