/*************************************************************************/
/* Recursion Example -Factorial                                          */
/* m.booth                                                               */
/* 2004 11 14 modified from Deital 2003 Fig 6.15                         */
/* ics4m                                                                 */
/*************************************************************************/

public class FactorialTest
{
   public static void main (String [] args)
   {
        //variables
        String strOutput; //output factorial answer
        int i;     //loop counter

        //output a title
        strOutput = "Factorial\n";

        // calculate the factorials of 0 through 10
        for ( i = 0; i <= 10; i++ )
        {
         strOutput = strOutput
                   + ( i + "! = "
                   + factorial( i ) + "\n" );
        }

        System.out.println(strOutput);
   }    //end of main

   // recursive declaration of method factorial
   //note use long type if overlow occurs
   static int factorial( int intNum )
   {
          //variables
          int intAns;//calculated answer

          // base case
          if ( intNum <= 1 )
          {
              intAns = 1;
          }
          else  // recursive step
          {
            intAns = intNum * factorial( intNum - 1 );
          }

          return intAns;

    } // end method factorial
} // end class FactorialTest

