logo Practice-It logo


Language/Type: Java recursion recursive programming
Author: Whitaker Brand

Write a method factorial that accepts an integer parameter n and that uses recursion to compute and return the value of n factorial (also known as n!). Your method should throw an IllegalArgumentException if n is negative. Several calls and their return values are shown below.

Call Output
factorial(0); 1
factorial(1); 1
factorial(3); 6
factorial(5); 120
factorial(10); 3628800
factorial(-4); IllegalArgumentException
Type your solution here:

This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.