Question

Implement the recursive approach in JAVA to raising a number to a power. Write a recursive...

Implement the recursive approach in JAVA to raising a number to a power.

Write a recursive power function and a main routine to test it.

So 28 = 2 * 27  = 2 * 2 * 26      and so on.

So x^Y is x multiplied by itself y times.

Homework Answers

Answer #1

Recursive approach in JAVA :

public class Main {
   public static void main(String[] args) {
      
      int x=2;
      int y=8;

      System.out.println("Value of ( "+x+" ^ " +y+" ) is : " +power(x,y));

   }
   public static int power(int x , int y)
   {
      if(y==0)
         return 1;

      int n = power(x , y -1);
      int ans = n*x ;
      return ans;


   }
}

SCREENSHOT OF ABOVE CODE:

EXAMPLE :

Know the answer?
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
Similar Questions
Please show the java code and pseudocode as well Describe and implement a recursive algorithm for...
Please show the java code and pseudocode as well Describe and implement a recursive algorithm for reversing a singly linked list L, so that the ordering of the nodes becomes opposite of what it was before. What is the running time of your algorithm on a list of n values? Provide both the growth function and BigOh complexity. Create a driver class to test your implementation.
In Java. Write a program that reads-in a times table-number. The program, using this table-number will...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will produce the following report (as shown). The first item in the report is a number with starting value 1. Second column is the word “ X ” (representing the times symbol). Third column is the table-number (itself). Following is an equal sign “ = “ (representing a result). Last column is the result of the operation for that line or row which is the...
The binary search algorithm given in this chapter is nonrecursive. Write and implement a recursive version...
The binary search algorithm given in this chapter is nonrecursive. Write and implement a recursive version of the binary search algorithm. The program should prompt Y / y to continue searching and N / n to discontinue. If an item is found in the list display the following message: x found at position y else: x is not in the list Use the following list of integers when testing your program: 2, 6, 8, 13, 25, 33, 39, 42, 53,...
In Java a function can call itself(we may explore more about this later in the course).  This...
In Java a function can call itself(we may explore more about this later in the course).  This is called a “recursive function”. In order for this to work there must be a situation where the function finally stopscalling itself and simply returns a value (or just returns). We can use a Java ifstatement to make a recursive function return as required to make it work properly. This special case where the function finally returns is called the recursive function base case....
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object>...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object> obj) that reverses an ArrayList of any type of object. For example, if an ArrayList held 4 strings: "hi", "hello", "howdy", and "greetings" the order would become "greetings", "howdy", "hello", and "hi". Implement a recursive solution by removing the first object, reversing the ArrayList consisting of the remaining Objects, and combining the two. Use the following class ArrayListReverser to write and test your program....
Write a complete recursive java program to compute the heights of the tick marks on a...
Write a complete recursive java program to compute the heights of the tick marks on a ruler. Assume that the length of the ruler is a power of 2 (say L=2^n, where n >=1) , and the marks are to be placed at every point between 0 and 2^n, not including the endpoints. The endpoints 0 and 2^n will have height 0. Here are the rules for computing the heights of the ticks for a ruler of length L=2^n: 1....
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
Please use basic Java program and comment so that I understand what is going on.   write...
Please use basic Java program and comment so that I understand what is going on.   write a class called lab1. (This will be your program.) In that class: Write the main method that calls multiConcat. Write a method called multiConcat that takes a String and an integer as parameters and prints a String that is the parameter string concatenated with itself n number of times (where n is the second parameter). For example, if the parameters are “hi” and 4,...
This is Java compute_pi (n) //n is the number of “darts” inside=0 for i = 1..n...
This is Java compute_pi (n) //n is the number of “darts” inside=0 for i = 1..n x=random() y=random() if sqrt(x*x+y*y)<=1: inside++ pi=4*inside/n return pi Create a new file called ComputePi.java, and implement the above pseudocode to estimate the value of pi. Test the code with increasingly-large values of n. You should not need to recompile the code to do this: use arguments at the commandline to pass in n, and extract the argument from args in main. (If you are...
java •Write a method product(int x, int y) which returns the product of multiplying x *...
java •Write a method product(int x, int y) which returns the product of multiplying x * y but doing it recursively. Recall that multiplication can be implemented as repeated addition. For example, 4 * 7 is the same as adding 7, 4 times: 7 + 7 + 7 + 7. •Write a main method that asks for two numbers, and returns their product by calling the product method. Compile and test your code in NetBeans and then on Hackerrank
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT