Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: help with recursive method

1. ## help 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:
```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.

2. ## Re: help with recursive method

Did you not like the advice given to you elsewhere? You should at the very least acknowledge that advice, and ask a question or elaborate if you do not understand.

This thread has been cross posted here: