Write a program which contains a main() method and a method multiplyEvens() that
ID: 3835402 • Letter: W
Question
Write a program which contains a main() method and a method multiplyEvens() that returns the product of the first n even integers.
For example: multiply Evens (1) returns 2 multiply Evens (2) returns 8 (2 4) multiply Evens (3) returns 48 (2 4 6) multiply Evens (4) returns 384 (2 4* 6 8) multiply Evens (5) returns 3840 (2* 4* 6*8* 10) Remark: You may NOT use a while loop, for loop or do/while loop to solve this problem, you MUST use recursion. The method should check whether the argument is greater than 0. public static int multiplyEvens (int n) if (n 1) throw new IllegalArgumentException else if (n- 1) return 2; else Fill your recursive code hereExplanation / Answer
int multiplyEvens(int n)
{
if(n < 1)
return 0;
else if(n == 1)
return 2;
else
{
return (n * 2) * multiplyEvens(n - 1);
//In recursion we must consider that the result up to n-1
//is already evaluated. In this case, we assume that we have the result
//till n-1. (n * 2) gives us the nth even number that we multiply
//with the evaluated result till n-1.
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.