/**
   The java core language is a type-safe imperative language with a C-like
   syntax.  The core language includes float's, int's, and (safe) arrays.  It
   does not include pointers, struct's, or unions, but these are all definable
   using objects and classes.

   To write C-like code, simply declare the functions as "static" methods of a
   class.  The static method main (yielding type void and taking an array of
   strings as argument) is the main entry point.
*/

class CoreDemo {

  /* Recursive factorial. */
  private static int rec_factorial (int n) {
    if (n==0) {
      return 1;
    } else {
      return n * rec_factorial (n-1);
    }
  }

  /* Recursive factorial, using a switch statement. */
  private static int rec_sw_factorial (int n) {
    switch (n) {
    case 0: return (1);
    default: return (n * rec_sw_factorial (n-1));
    }
  }

  /* Factorial, computed using a for loop. */
  private static int for_factorial (int n) {
    int result = 1;
    for (int i=n; i!=0; i--) {
      /* Invariant: result = n!/i! */
      result *= i;
    }
    return result;
  }

  /* Factorial, using a while loop. */
  private static int while_factorial (int n) {
    int result = 1;
    int i = n;
    while (i != 0) {
      /* Invariant: result = n!/i! */
      result *= i;
      i--;
    }
    return result;
  }

  /* Main entry point. */
  public static void main (String argv[]) {
    int x =
      argv.length == 0 ? 10 : Integer.parseInt(argv[0]);
    int y =
      while_factorial (x);
    System.out.println ("Factorial of " + x + " is " + y);
  }

}
