/** * A very small program for computing the factorial * of a number * * @author gtowell * Created: Oct 18, 2021 */ public class Factorial { public static void main(String[] args) { String[] defaultArgs = { "5" }; if (args.length == 0) args = defaultArgs; try { int fb = Integer.parseInt(args[0]); Factorial f = new Factorial(); int b = f.factorial(fb); System.out.println(b); } catch (NumberFormatException nfe) { System.err.println("<<" + args[0] + ">> must be an integer"); } } /** * Compute factorial recursively * @param n the number whose factorial you want to compute * @return the factorial of the given number */ public int factorial(int n) { if (n == 1) return 1; if (n < 1) return 0; int f = factorial(n - 1); return f*n; } }