gno/usr.bin/awk/tests/factorial.awk
tribby ddb82cb2e0 Remaining files for awk 2.0 that were left out of the previous checkin.
Maybe someday I'll become adept at using cvs...
1998-04-07 17:06:53 +00:00

23 lines
512 B
Awk

# Calculate factorial of command-line argument
BEGIN {
# Get the command-line argument
if (ARGC < 2) {
print "ERROR: please provide number as command-line parameter"
exit 1
}
n = ARGV[1]; ARGV[1] = "";
# Print heading
print "Calculating",n,"factorial"
# Start the recursion
nf = factorial(n);
# All done; print the result
print "Result:",nf;
}
# Recursive function
function factorial(n) {
if (n <= 1) return 1;
else return n*factorial(n-1);
}