Recursion Part //1. Write out the output for eaching of following: /* Consider this function. void myMethod( int counter) { if(counter == 0) return; else { System.out.println(\"\"+counter); myMethod(--counter); return; } } This recursion is not...

50.1K

Verified Solution

Question

Programming

RecursionPart

//1. Write out theoutput for eaching of following:

/*Consider this function.

voidmyMethod( int counter)

{

if(counter== 0)

return;

else

{

System.out.println(\"\"+counter);

myMethod(--counter);

return;

}

}

This recursion isnot infinite, assuming the method is passed a positive integervalue. What will the output be?

b.Consider thismethod:

voidmyMethod( int counter)

{

if(counter== 0)

return;

else

{

System.out.println(\"hello\"+ counter);

myMethod(--counter);

System.out.println(\"\"+counter);

return;

}

}

Ifthe method is called with the value 4, what will the output be?Explain.

c.

publicclass Recursion {

publicstatic void mystery1(int a, int b) {

if(a <= b) {

intm = (a + b) / 2;

System.out.print(m+ \" \");

mystery1(a,m-1);

mystery1(m+1,b);

}

}

publicstatic void mystery2(int n) {

if(n > 0) {

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

mystery2(n-2);

mystery2(n-3);

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

}

}

publicstatic void mystery3(int n) {

if(n == 0 || n == 1) return;

mystery3(n-2);

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

mystery3(n-1);

}

publicstatic String mystery4(int n) {

if(n <= 0) return \"\";

returnmystery4(n-3) + n + \" \" + mystery4(n-2) + n + \" \";

}

publicstatic void main(String[] args) {

intN = 5;

mystery1(0,N);

System.out.println();

mystery2(N);

System.out.println();

mystery3(N);

System.out.println();

System.out.println(mystery4(N));

}

}

Answer & Explanation Solved by verified expert
4.5 Ratings (574 Votes)
HiHope you are doing fine I have executed the above three codesto fetch their execution and output Although an explanation isasked for only question b if have provided the explanation forall the 3 codes The explanation for code 3 is quite difficult asit undergoes a lot of recursive calls and returns I have stilltries my best to give you an idea of how the recursive callsoccurQuestion aThe program prints the list of numbers starting from the enteredvalue N to 1 decreasing order with every number in new line    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students