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));
}
}