Lexington High School Mathematics Math Dept. Front Page  | Student Links  | Family Links 
Introduction to C++, Kevin Kelly Kelly Front Page  | C++ Course Guide | C++ Assignments | C++ Info

Assignment 19: Factorial function

due Monday 4/1

The factorial of a positive whole number n is defined to be the product of all the whole numbers from n down to 1. That is,

"n factorial" = n * (n-1) * (n-2) * ... * 2 * 1.
Write a function called factorial that returns the factorial of any positive whole number.

Your function is required to use recursion (in other words, the factorial function must include a call to itself). What does recursion have to do with factorials? This question provides a hint: How is n factorial related to (n-1) factorial?

Also write a main program that allows you to test your function, and submit enough test runs to provide convincing evidence that your function works.

Extensions

(This one is relatively easy. Try it even if you don't usually try the extensions.)

For 10 pts.: It doesn't come from the definition above, but mathematicians usually define "0 factorial" to be equal to 1. Modify your function so that factorial(0) produces the value 1.