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
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!!!
Write a recursive method: public static int raise2ToN(int n) //computes and returns the value of 2n...
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 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...
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 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 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 index of n in the Fibonacci series. If n does not exist, the method returns -1. C#
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 (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
Java, how would i do this public static void main(String[] args) { int n = 3; int result; result = factorial(n); + public static int factorial(int n) public static int factorial(int n) n = 3 { returns 3* 2 * 1 { if (n == 1) return n; else return (n * factorial(n-1)); if (n == 1) return n; else return (3 * factorial(3-1)); ܢܟ } public static int factorial(int n) n = 2 public static int factorial(int n) returns...
Question 111 pts Given the method definition: public int spin( int n ) { if ( n <= 1 ) return 0; if ( n % 2 != 0 ) return spin( n - 1 ); return n + spin( n - 2 ); } What is the value of the following expression? spin( 7 ) 0 7 10 12 15 Flag this Question Question 121 pts What does the spin() method do? public int spin( int n ) {...