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.

  • 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...

  • 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...

  • Write a MIPSzy subroutine, power, that accepts 2 arguments x and y and computes x^y ....

    Write a MIPSzy subroutine, power, that accepts 2 arguments x and y and computes x^y . You can assume that x and y are both larger than 0. The main program passes the two parameters and receives the return value through the program stack. addi $t0, $zero, 5100 top: lw $t1, 0($t0) beq $t1, $zero, done addi $sp, $sp, -4 sw $t1, 0($sp) addi $t2, $zero,4 addi $sp, $sp, -4 sw $t2, 0($sp) addi $sp, $sp, -4 jal power lw...

  • ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns...

    ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns a single, merged sorted list as a result. For example, if the two input lists are [0, 2, 4, 6, 8] and [1, 3, 5, 7, 9], the returned result should be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Remember that you can add lists in Python using the + operator ([0] + [1,2,3] = [0,1,2,3]) and can take slices of...

  • *In JAVA please* Tasks This lab has two parts: Write a recursive method that converts a...

    *In JAVA please* Tasks This lab has two parts: Write a recursive method that converts a decimal number to a different base number system. Print out the results after testing your method on a few different inputs. Task 1 – Recursive Method Create a recursive method that returns a given number converted from base ten to a given other base number system ranging from two to thirty-six. A decimal number, or base ten number, can be expressed in any other...

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
ADVERTISEMENT