Question: Exercise 1 : Factorials, done recursively. Create a function called factorial, defined as follows: int factorial ( int theNum ) ; This function should take

Exercise 1: Factorials, done recursively.
Create a function called factorial, defined as follows:
int factorial(int theNum);
This function should take an integer argument and return an integer result. It should work as follows:
If given a number less than zero, it should return a value of -1, representing an error.
If given a 0 or 1, it should return a value of 1.
If given a number larger than 1, it should compute the factorial of the number minus 1, and
multiply it by that number. (For example, for factorial(6), it would return the product of 6 times
factorial(5). This is a direct analog to the idea of mathematical induction.
Don't use any for() or while () loops in your function; if done recursively, you don't need any.
It's OK(and expected) to use a for() loop in main() to test your function.
It is possible to do this in three lines of code (and probably even fewer), but this is not required.
Create a testbench program - main() and all that - to test your factorial function. What is the highest
number for which it can correctly compute the factorial?
Now, replace all of your type int with type unsigned long long. (Remember to do this for the function
parameters, as well as the individual variables. Also recall you will need "%llu" for the printf() format.
Does your program work differently now? Does it work for larger numbers? What is going on?
 Exercise 1: Factorials, done recursively. Create a function called factorial, defined

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!