previous | start | next

Simple Application: compute factorial

/** Computes and displays n! */
public class Factorial {

    /** main function
      * @param args the command line arguments, stored in a String array
      */
    public static void main(String[] args) {
        int f = 1;

        // convert command line argument to integer type:
        int n = Integer.parseInt(args[0]);

        // calculate factorial
        while (n > 1) {
            f = f * n;
            n--;
        }

        // display result:
        System.out.println(args[0] + "! = " + f);
    }
}



previous | start | next