SHARE
TWEET

Untitled

advictoriam Jan 11th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. /**
  4.    Computes x^n (x raised to the nth power). Although the value
  5.    of x can be positive or negative, the value of the integer n
  6.    should be non-negative.
  7.  
  8.    Input: x, the double value to be raised to the nth power.
  9.    Input: n, the integer value to which x is to be raised.
  10.    Output: x^n -- the value of x raised to the nth power.
  11. */
  12. public class Power
  13. {
  14.    public static void main(String[] args)
  15.    {
  16.       // Read value for n
  17.       Scanner in = new Scanner(System.in);
  18.       double x = in.nextDouble();
  19.       int n = in.nextInt();
  20.       if(n == 0){System.out.println("1.0"); return;}
  21.       System.out.println(power(x, n));
  22.    }
  23.    
  24.    public static double power(double x, int n)
  25.    {
  26.       if(n == 1){return x;}
  27.       return x * power(x, --n);
  28.    }
  29. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top