public class Factorial {
/** Compute and return x!, the factorial of x. */
public static int factorial(int x) {
int fact = 1;
for (int i = 2; i <= x; i++ ) {
fact *= i;
}
return fact;
}
}
I type "java Factorial 3" in the command window and it does not return 3! (or any other x!). Instead it gives an error:
"Static Error: No method in static Factorial has name 'main' "...
I copied it from a book which says that it isn't a program but a method itself.
What should I do so that the command window may return the factorial?
I wanted to be ideal,... but I came to know that it's just an idea!
Your class has no main() method which is the starting point of execution for a Java class. Can you add the main method and try calling the factorial() method from it.
Can you search in the book & find how to write a main method and call the factorial method you already have?
Welcome... Click "My Profile" link which you see at the top under the "The Big Moose Saloon" site poster. And give a signature under General Information about yourself tab in your profile edit page.