Question

Write a recursive method: public static int raise2ToN(int n) //computes and returns the value of 2n...

Write a recursive method:

public static int raise2ToN(int n)

//computes and returns the value of 2n using

//recursion.

Note: 2^3 = 2 * 2^2 .

2^2 = 2 * 2^1

2^1 = 2

What value/values of n will stop the recursion (base case)?

Sample output:

5

2 to 5 is 32

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM:

import java.util.*;
public class Main
{
   public static int raise2ToN(int n)
{
if(n==0)
{
return 1;
}
else
{
return 2*raise2ToN(n-1);
}
}

   public static void main(String[] args)
   {
       Scanner src=new Scanner(System.in);
       int num=0;
       System.out.print("Enter the value of n: ");
       num=src.nextInt();
       int res=raise2ToN(num);
       System.out.println("2 to "+num+" is "+res);
   }
}
OUTPUT:

This Programs contains a recursive method, which accepts the n value and calculate the 2n value .

In the method, we are continuously multiplying with 2 and decreasing the value of n until we reach the value of n as 0.

Hope this helps!!!

Add a comment
Know the answer?
Add Answer to:
Write a recursive method: public static int raise2ToN(int n) //computes and returns the value of 2n...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Write a recursive function that computes the sum of all numbers from 1 to n,...

    1. Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter. Here is the method header: public static int sum (int n){...} 2. Write a recursive function that finds and returns the minimum value in an array, where the array and its size are given as parameters. Here is the method header: public static int minValue (int [] data, int size){...} 3. Write a recursive function that reverses the...

  • Write a public static method named getMaxOf2Ints that takes in 2 int arguments and returns the...

    Write a public static method named getMaxOf2Ints that takes in 2 int arguments and returns the Maximum of the 2 values Write a public static method named getMinOf2Ints that takes in 2 int arguments and returns the Minimum of the 2 values Write apublic static method named getMaxOf3Ints that takes in 3 int arguments and returns the Maximum of the 3 values Write a public static method named getMedianOf3Ints that takes in 3 int arguments and returns the Median Value...

  • Why might the following method have infinite recursion? public int infiniteRecursion(int n) { if (n >...

    Why might the following method have infinite recursion? public int infiniteRecursion(int n) { if (n > 0) { return infiniteRecursion(n) - 2; } else { return 0; } } Because the base case will never be true None of these are correct, there is no infinite recursion in this method Because there is no base case Because the recursive call does not move the parameter closer to the base case

  • c++ problem Write a recursive method int mul(int a, int b) that computes the product of...

    c++ problem Write a recursive method int mul(int a, int b) that computes the product of two nonnegative integers a and b. You are not allowed to use multiplication () Hint: use addition (+) and recursion. Write a method evenDigits that accepts an integer parameter n and that returns the integer formed by removing the odd digits from n. The following table shows several calls and their expected return values: 1- 2- Call Valued Returned evenDigits (8342116); 8426 evenDigits(4109); 4...

  • The following recursive method factRecursive computes the factorial of positive integer n. Demonstrate that this method...

    The following recursive method factRecursive computes the factorial of positive integer n. Demonstrate that this method is recursive. public static int factRecursive(int n) { int result = 0; if (n == 0) { result = 1; } else { result = n * factRecursive(n - 1); } return result; }

  • Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the...

    Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the nth F(n) using recursive algorithm (i.e., recursive function call). Fibonacci numbers are defined by F(1)=F(2)=1, F(i) = F(i-1)+F(i-2), i=2,… . Function int iterative_fibonacci(int n) computes and returns the nth Fibonacci number F(n) using iterative algorithm (i.e., loop). The main function measures the memory usage and run time of iterative_fibonacci(40) and recursive_fibonacci(40), and does the comparison. To capture the execution time by millisecond, it needs...

  • public static int Fibonacci(int n) This method receives an integer as a parameter and returns the...

    public static int Fibonacci(int n) This method receives an integer as a parameter and returns the index of n in the Fibonacci series. If n does not exist, the method returns -1. C#

  • In Java Write a recursive method called sumUpto(n) which will return the sum of the first...

    In Java Write a recursive method called sumUpto(n) which will return the sum of the first n integers. For example, sumUpto(4) will return 10 because 1+2+3+4 = 10. sumUpto(5) will return 15 because 1+2+3+4+5=15. So you can see that sumUpto(5) = sumUpto(4) + 5. Make this more general for n : sumUpto(n) = sumUpto(??) + n Figure out the base case and write the method……….. import java.util.Scanner; public class Lab12Num1 { public static int sumUpto(int num) {    } public...

  • in java Part III: Permutations (10 points) Write a recursive method public static ArrayList<int[] > permuteArray...

    in java Part III: Permutations (10 points) Write a recursive method public static ArrayList<int[] > permuteArray (int[] array) that returns an ArrayList of all permutations of the the parameter array. The ArrayList may contain the ar- rays in any order. Example: Suppose array = {4, 7, 1, 2}. Then the ArrayList would contain the following arrays, but in any order: 4 7 12 7 4 1 2 2 1 4 7 1 24 7 4 7 2 1 7 4...

  • on 2 Consider the following recursive method test: public static int test(String s, int last) if...

    on 2 Consider the following recursive method test: public static int test(String s, int last) if (last < 0) { return 0; if (s.charAt(last) == 0) { return 2 - test(s, last - 1); return 1 + 2 test(s, last - 1); What is the output of: test("11001", 4). TTT Arial 3 (12pt) - TEE 25

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT