Write a recursive definition for the following methods and t
Write a recursive definition for the following methods and test the methods
1. Compute the product of n integers
productInteger(n) = 1*2*3*4*5*…*n (n >=1)
• Test it with productInteger(4)
2. Compute the product of n even integers
productEven(n) = 2*4*6*8*10*…*[2*(n-1)]*[2*n] (n >=1)
• Test it with productEven(5)
3. Compute the product of n odd integers
productOdd(n) = 1*3*5*7*9*...*[2*(n-1) - 1]*[2*n -1] (n >=1)
• Test it with productOdd(6)
Using Java standard codes, write a program for this question. Also, use comments to explain the lines of codes
Solution
public class Recursion {
static int productInteger(int n)
{
if(n==1)
{
return 1;
}
int ans=1;
ans = productInteger(n-1)*n;
return ans;
}
static int productEven(int n)
{
if(n==1)
{
return 2;
}
int ans=1;
ans = productEven(n-1)*2*n;
return ans;
}
static int productOdd(int n)
{
if(n==1)
{
return 1;
}
int ans=1;
ans = productOdd(n-1)*(2*n-1);
return ans;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(productInteger(4));
System.out.println(productEven(5));
System.out.println(productOdd(6));
}
}

