problem with recursive method

My problem is with this exercise:

Write a recursive method called power that takes a double x, and an integer n and that returns x^n. Hint: a recursive definition of this operation is x^n = x*x^(n-1). Also, remember that anything raised to the zeroeth power is 1. Optional challenge: you can make this method more efficient, when n is even, using x^n=(x^(n/2))^2.

Now call me crazy but I just can't see why recursion is necessary or how it could be implemented into this, here is the code I wrote:

Code:

`package edu.vtc.aav10260.cis2261;`

import java.util.Scanner;

/**

* @author andre

*

*/

public class Power {

/**

* @param args

* @return

*/

public static double main(String[] args) {

Scanner kbd = new Scanner(System.in);

System.out.println("Enter the values: ");

System.out.print("x= ");

double x = kbd.nextDouble();

System.out.print("n= ");

int n = kbd.nextInt();

return Math.pow(x, n);

}

}

It works, and returns x^n just as the exercise said, but it specified to use a recursive method so if any of you guys could give me a hand in how recursion would work with this problem it would be appreciated.

Re: problem with recursive method

A recursive method calls itself, so if you need to calculate the power of a number, how do you think you can do so using a method that calls itself? Break the problem down....what is the definition of power a ^ b ? Use an example where a = 3, b = 4: 3 ^ 4 = 3 * 3 * 3 * 3. In other words, 3 times itself 4 times...so what could you put inside a method that calls itself to calculate a times itself b times?

Re: problem with recursive method

Re: problem with recursive method

Evidently you don't care for advice given, in fact completely ignored it, which doesn't help your chances of receiving help here or elsewhere. And neither does cross-posting.

Cross posted at help with recursive method

Re: problem with recursive method

i didnt really understand what you were saying

Re: problem with recursive method

Quote:

Originally Posted by

**mflb94** i didnt really understand what you were saying

Then ask a question about it rather than ignoring it, because ignoring is a 2-way street.

Re: problem with recursive method

ok well my question is, will you please show me how you would write this code?

Re: problem with recursive method

Quote:

Originally Posted by

**mflb94** ok well my question is, will you please show me how you would write this code?

That's not how it's done here since we don't do other people's homework. Surely you've studied your text or a tutorial on recursion. Surely you've trued to solve this first yourself. so what about recursion confuses you? What doesn't work in your code attempt?

Re: problem with recursive method

i just don't see how recursion can be used in this problem as I said the code that I wrote did exactly what the exercise says to and doesn't need recursion

Re: problem with recursive method

Quote:

Originally Posted by

**mflb94** i just don't see how recursion can be used in this problem as I said the code that I wrote did exactly what the exercise says to and doesn't need recursion

Just because you can solve it non-recursively doesn't mean that there isn't a recursive solution. The key as has been mentioned before is to work it out on paper first. But before you do that, see how other recursive methods, like the one for calculating Fibonacci numbers works.