Question
The product of n can be written as illustrated below, where productOf(1) = 1 is the base case for the recursive formulation of this computation.
The product of n can be written as illustrated below, where productOf(1) = 1 is the base case for the recursive formulation of this computation.
productOf(n) = n * (n-1) * ... * 3 * 2 * 1
for any integer n > 0
productOf (n) = n * productOf(n-1)
productOf(1) = 1
---------------------------------------------------------------------
In java this recursive computation can be programmed in two ways shown below. The difference is that the intermediate value of product is not stored.
public static int productOf(int n)
{
int product;
if ( n==1)
product = 1;
else product = n * productOf(n-1);
return product;
}
---------------
public static int productOf(int n)
{
if ( n==1)
return 1;
else
return n * productOf(n-1)
}
Use both examples with n = 10 and write a main method so both examples of the code work. Annotate the code to show that you understand the program(s) and submit. Note that no credit will be given for submissions that do not compile/run or are not correctly annotated. **java**
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started