Question: JAVA help Exercises in recursion: What output is produced by the following: public class Q1 { public static void main(String args[]) { methodA(3); System.out.println(); }

JAVA help

Exercises in recursion:

What output is produced by the following:

public class Q1

{

public static void main(String args[])

{

methodA(3);

System.out.println();

}

public static void methodA(int n)

{

if (n<1) System.out.print('B');

else

{ methodA(n-1);

System.out.print('R');

}

}

}

What output is produced by the following:

public class Q2

{

public static void main(String args[])

{

methodB(3);

System.out.println();

}

public static void methodB(int n)

{

if (n<1) System.out.print('B');

else

{

System.out.print('R');

methodB(n-1);

}

}

What output is produced by the following:

public class Q3

{

public static void main(String args[])

{

System.out.println(getMysteryValue(3));

}

public static int getMysteryValue(int n)

{

if (n<=1) return 1;

else

{

return getMysteryValue(n-1) + n;

}

}

}

What will the output be if the code in main is replaced by

System.out.println(getMysteryValue(6));

What output is produced by the following:

public class Q5

{

public static void main(String args[])

{

System.out.println("The output is:");

f(35);

System.out.println();

}

public static void f(int number)

{

if (number > 0)

{

f(number/2);

System.out.print(number%2);

}

}

}

What output is produced by the following:

public class Q6

{

public static void main(String args[])

{

System.out.println("The output is:");

bar(11156);

System.out.println();

}

public static void bar(int number)

{

if (number > 0)

{ int d = number%10;

boolean odd=(number/10)%2==1;

bar(number/10);

if(odd)

System.out.print(d/2 + 5);

else System.out.print(d/2);

}

}

}

What output is produced by the following:

public class Q7

{

public static void main(String args[])

{

System.out.println(f(6));

}

public static int f(int n)

{

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

else return 2*f(n-2) + f(n-1);

}

}

The function in Q7 will produce a series.

Find the first 8 numbers in the series by calculating f(1), f(2), f(3), f(4), f(5), f(6), f(7), f(8)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!