Factorial.java


Below is the syntax highlighted version of Factorial.java.


/*************************************************************************
 *  Compilation:  javac Factorial.java
 *  Execution:    java Factorial N
 *
 *  Reads in a command-line argument N, and prints N! = 1 * 2 * ... * N
 *  to standard output.
 *
 *  % java Factorial 0
 *  1
 *
 *  % java Factorial 1
 *  1
 *
 *  % java Factorial 5
 *  120
 *
 *  % java Factorial 12
 *  479001600
 *
 *  % java Factorial 20
 *  2432902008176640000
 *
 *
 *  Remarks
 *  -------
 *   - Would overflow a long if N > 20
 *   - Need to use extended precision arithmetic to handle bigger factorials
 *
 *************************************************************************/

public class Factorial {
    
    // recursive method to find factorial
    // assumes n >= 0 and n <= 20
    public static _______ factorial(__________________________________) {

         // base case
         if (________________)

              return _________;


         // reduction step
         return ______ * factorial(________);

    }


    public static void main(String[] args) {
        long N = Long.parseLong(args[0]);
        System.out.println(factorial(N));
    }

}