FileDocCategorySizeDatePackage
factorial.javaAPI DocExample1246Thu Jun 07 19:51:02 BST 2001None

Factorial

public class Factorial extends Object
This program computes the factorial of a number

Fields Summary
Constructors Summary
Methods Summary
public static doublefactorial(int x)

  // This method computes x!
    if (x < 0)                             // Check for bad input
      return 0.0;                          //   if 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
  
public static voidmain(java.lang.String[] args)

 // The program starts here
    int input = Integer.parseInt(args[0]); // Get the user's input
    double result = factorial(input);      // Compute the factorial
    System.out.println(result);            // Print out the result