Question: Given the factorial number sequence: 1 4 9 16 25 36 49 64 One of your friends took the recursive factorial functions and tried a
Given the factorial number sequence:
1 4 9 16 25 36 49 64
One of your friends took the recursive factorial functions and tried a different way using a pattern in the number sequence. The first term represents 1!, 2nd term is 2!, and so forth making the nth term representing n!.
After reviewing the code, you find there is one line of code with a problem. Show your friend your analysis by answering the following questions:
What is the code going to return when you call it with factorial(4)?
What line of code would you change to fix the function?
// Assume n > 0. Ignore calculating 0!
public int factorial(int n)
{
if (term == 1)
return 1;
return factorial(term 1) + (term + 2) - 1;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
