/*
 * Power.java by Richard J. Davies
 * from `Introductory Java for Scientists and Engineers'
 * chapter: `Java from Basics'
 * section: `Example - Integer Powers'
 *
 * This program contains a method `power' which raises a floating point number
 * to an integer power. It is intended as a demonstration of the syntax for
 * defining and using a method.
 */
public class Power
{
  // The method 'power' raises x to the power
  // of n.
  public static double power(double x, int n)
  {
    if (n == 1)
    {
      return x;
    }
    else
    {
      // Integer division rounds down
      double value = power(x, n / 2);

      if (n % 2 == 1)
      {
        return x * value * value;
      }
      else
      {
        return value * value;
      }
    }
  }


  // For our main method, just compute a power
  // of two - the point of this example is in the
  // preceding method.
  public static void main(String[] argv)
  {
    System.out.println(power(2,10));
  }
}

