% javac <file>.java -- this outputs a new filewhere file <file>.java contains the Java source code and.class % java <name> [<command line arguments>]
// Put this program in a file called Factorial.java
// Then invoke
// % javac Factorial.java
// % java Factorial
//
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));
}
}
|