
/**
 * A program that uses recursion to calculate factorials.
 *
 * @author Mark Young (A00000000)
 */
public class Factorial {

    public static void main(String[] args) {
        System.out.println("\n"
                + "The factorials from 1 to 10\n"
                + "---------------------------\n");
        for (int i = 1; i <= 10; ++i) {
            System.out.println(i + "! == " + factorial(i));
        }
        System.out.println("\n\n");
    }

    /**
     * Calculate the factorial of n.
     * (Uses recursion.)
     *
     * @pre  n >= 0 and n! <= Integer.MAX_VALUE
     * @post return value is n!
     *
     * @param n the number to calculate the factorial of.
     * @reurn the factorial of n (n!)
     */
    public static int factorial(int n) {
        if (n <= 1) {
            return 1;
        } else {
            return n * factorial(n - 1);
        }
    }

}
