/* file: Factorial.java * * Basic Algorithms, V22.0310.003, Fall98 (Yap) * Modified: Fall99 * $Id: Factorial.java,v 1.1 1999/09/14 21:58:46 yap Exp 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) { // factorial function if (k<0) return -1; else if (k == 0 || k == 1) return 1; else return k * computeFactorial(k-1); } // computeFactorial public static void main(String[] args) { // main function int n; n = Integer.parseInt(args[0]); for (int i = 0; i <= n; i++) System.out.println (i + " factorial is " + computeFactorial(i)); } // main }