AP Computer Science --- Haas --- RecursiveFactorial

Objective: Understand a simple recursive method.

Run the following class in BlueJ make sure you understand how it works.



public class RecursiveFactorial{

  public static int factorial( int n )  {
     if ( n <= 0 )  // base case
        return 1;
     else    // general case
        return ( n * factorial ( n - 1 ) );
  }

  public static void main( String [] args )  {
    // compute factorial and output it
    System.out.println( "4! = " + factorial(4) );
    System.out.println( "1! = " + factorial(1) );
    System.out.println( "6! = " + factorial(6) );
  }
}