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.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: help with recursive method

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default 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:


    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

Similar Threads

  1. [SOLVED] how to trace recursion variables in a recursive method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 12
    Last Post: May 23rd, 2012, 12:00 PM
  2. Problem with recursive method. Can you help?
    By TFLeGacY in forum Algorithms & Recursion
    Replies: 6
    Last Post: December 7th, 2011, 05:44 PM
  3. Problem with fairly basic recursive method
    By TFLeGacY in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 6th, 2011, 09:58 PM
  4. How do I connect 2 method headers to make it recursive???
    By tripline in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 5th, 2011, 05:27 AM
  5. [SOLVED] StackOverflowError with recursive method
    By samfin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 2nd, 2010, 04:05 PM