/** * This program computes and prints the factorial of a number pass as an argument on the command line. Usage: java Factorial */ public class Factorial { // Define a class public static void main(String[] args) { // The program starts here if (!(args.length==1)) { // Right number of args? System.out.println("Usage: java Factorial "); } else { int input = Integer.parseInt(args[0]); // Get the user's input System.out.println(factorial(input)); // Call facorial method, print result } } // The main() method ends here public static double factorial(int x) { // This method computes x! if (x<0) return 0.0; // if input's bad, return 0 double fact = 1.0; // Begin with an initial value while(x>1) { // Loop until x equals 1 fact = fact*x; // multiply by x each time x = x-1; // and then decrement x } // Jump back to the star of loop return fact; // Return the result } // factorial() ends here } // The class ends here