// Basic Algorithms, Fall98 (Yap) /** * Factorial computes the factorial function * It also illustrates the processing of command line arguments. * * The main function expects an integer whose factorial will be * computed. If you put this file in a file called Factorial.java, * then you can compile and run this program as follows: * * % javac Factorial.java * % java Factorial 5 * * The program will then compute 5! (= 120) */ class Factorial { static int computeFactorial(int k) { if (k<0) return -1; else if (k == 0 || k == 1) return 1; else return k * computeFactorial(k-1); } public static void main(String[] args) { int n; n = Integer.parseInt(args[0]); for (int i = 1; i <= n; i++) System.out.println (i + " factorial is " + computeFactorial(i)); } }