Question

RecursiveExponent.java

Write a recursive function that accepts two arguments into parameters x and y, and which returns the value of x raised to the power y. Hint: exponentiation is equivalent to performing repetitions of a multiplication. For example, the base 4 raised to the 6th power = 4*4*4 * 4 * 4 * 4 = 4,096. The only file that you need to create is RecursiveExponent.java. Your main method should prompt the user to supply the base and exponent values (as integers), and then invoke the recursive method to calculate the value x raised to the power y. Sample outputs are shown in figures 1 and 2. You MUST write your own recursive function. You CANNOT use any of the static methods from the Math class. The main method of this programming task can be written in as few as 6 lines, and the recursive method, in as few as 6 lines. If you find yourself writing much more than that, then stop, and refer back to the lecture slides. Recall that a recursive method needs a base case, and when the base case is not met, then the recursive method reduces the problem to a smaller one. What is the base: 4 What is the exponent: 6 4 raised to the power 6 is 4096 Figure 1: Sample invocation of RecursiveExponent.java What is the base: -3 What is the exponent: 5 -3 raised to the power 5 is -243 Figure 2: Sample invocation of RecursiveExponent.java, when the input base is a negative integer

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

import java.util.Scanner;

public class RecursiveExponent {

public static void main(String[] args)

{

long base, exp;

Scanner kb = new Scanner(System.in);

System.out.print("What is the base: ");

base = Long.parseLong(kb.nextLine());

System.out.print("What is the exponent: ");

exp = Long.parseLong(kb.nextLine());

long result = getResult(base, exp);

System.out.println(base + " raised to the power " + exp + " is " + result);

}

public static long getResult(long base, long exp)

{

if(exp == 1)

return base;

else

exp--;

return base * getResult(base, exp);

}

}

OUTPUT

Important Note: As per HOMEWORKLIB RULES we are advised to attempt one question in a single post. Hope you will understand the issue and will not downvote.

Add a comment
Know the answer?
Add Answer to:
RecursiveExponent.java Write a recursive function that accepts two arguments into parameters x and y, and which...
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
  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • Recursive Multiplication in Python Design a recursive function that accepts two arguments into the parameters x...

    Recursive Multiplication in Python Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 6 × 4 = 4 + 4 + 4 + 4 + 4 + 4

  • PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the...

    PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the result of integer division (i.e., 8 / 3 = 2). Please comment on every line of code. EXISITNG CODE: int Exponentiation(int X, int Y) { if (Y == 0) // base case return 1; else // recursive case return X * Exponentiation(X, Y - 1); //make recursive call } int Multiply(int X, int Y){ if(Y == 0){ return 0; } else{ return X +...

  • Data structure c++ Write a recursive function, power, that takes two integers x and y as...

    Data structure c++ Write a recursive function, power, that takes two integers x and y as parameters such that x is nonzero and returns x'. You can use the following recursive definition to calculate xy: If y 20, y = 0; 1, power(x,y)= x, ( xx power(x, y-1), If y<0, y>1. power(x, y) = power(x,-y) Also, write a main function to test your function.

  • PROBLEM: Write a recursive method named SumEvens that takes an integer X and returns the sum...

    PROBLEM: Write a recursive method named SumEvens that takes an integer X and returns the sum of even digits in X. Please comment on every line of code. EXISTING CODE: int numTwos(int n) { int right = n % 10; int remain = n / 10; if(n==0) { return 0 ; } else if(right ==2) { //rightmost digit is 2 return 1 + numTwos(remain); else { return 0 + numTwos(remain); } } int SumDigits(int X) { if (X == 0)...

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

  • PROBLEM: Write a recursive method named Addition that takes two integers X and Y returns their...

    PROBLEM: Write a recursive method named Addition that takes two integers X and Y returns their sum. Please comment on every line of code. EXISTING CODE: int AddToN(int n) { if (n == 0) { // base case return 0; } else { // recursive case return n + AddToN(n-1); //make recursive call } } void printStars (int n) { if (n==0 ) { //base case cout <<""; } else{ printStars(n-1); cout << ""; } } } int sumRange (...

  • Need in Python! Write a function definition named  power  that takes two formal parameters which can be assumed...

    Need in Python! Write a function definition named  power  that takes two formal parameters which can be assumed to be any numeric type. The first parameter is called base, the second is called exponent. The function should return the result of the base raised to the exponent power.    You may not use any Python math library functions inside your function. You may create your own code to test the function in an IDE if you wish.   HINT: The exponent operator in...

  • In C++, write a recursive function power that takes two positive integers base and n as...

    In C++, write a recursive function power that takes two positive integers base and n as inputs and computes base to the n power. Example power(3, 2) is 9. Do not use any loops in your program. This is what I got so far but I'm not sure what I'm doing wrong: #include <iostream> using namespace std; int calc(int x, int y); int main() {       calc(2, 3);    return 0; } int calc() {          cout...

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