Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Write a recursive definition for the following methods and test the methods

ID: 3765353 • Letter: J

Question

Java

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(20)

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(21)

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(22)

OUTPUT has to show(example for: productOdd(2)=1*2=2)

Explanation / Answer

public class Test {

   static int productInteger(int n) {

       if (n==1) {

           System.out.print(1);

           return 1;

       }

      

       int temp = n*productInteger(n-1);;

       System.out.print("*"+n);

       return temp;

   }

  

   static long productEven(int n) {

       if (n==1) {

           System.out.print(2);

           return 2;

       }

       long temp = 2*n*productEven(n-1);

       System.out.print("*"+2*n);

       return temp;

   }

   static long productOdd(int n) {

       if (n==1) {

           System.out.print(1);

           return 1;

       }

       long temp = (2*n -1)*productOdd(n-1);

       System.out.print("*"+(2*n-1));

       return temp;

   }

   public static void main(String[] args) {

      

       System.out.print("productInteger(4) = ");

       int pro=productInteger(4);

       System.out.println("="+pro);

      

       System.out.print("productEven(4) = ");

       long pro2=productEven(4);

       System.out.println("="+pro2);

      

       System.out.print("productOdd(4) = ");

       long pro1=productOdd(4);

       System.out.println("="+pro1);

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote