As I am very new to Java Programming, I'd like to make sure that this code is right!
I was trying to make a program that computes the factorial of an input integer (args[0]) using a recursive method factorial():
The following is my program
public class FactorialDemo {
public static void main(String[] args) {
int k=Integer.parseInt(args[0]);
System.out.println(k + " factorial is " + factorial(k));
}
static double factorial(int n) {
if (n==1) return 1;
else return n*factorial(n-1);
}
}
I hope i have written it right, however when I complile and run, there is an Exception "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0". I tried changing static type to double and long. I also tried adding "import java.util*;" What difference would it make to this program? can someone please help, thanks.