Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- Computes x^n (x raised to the nth power). Although the value
- of x can be positive or negative, the value of the integer n
- should be non-negative.
- Input: x, the double value to be raised to the nth power.
- Input: n, the integer value to which x is to be raised.
- Output: x^n -- the value of x raised to the nth power.
- */
- public class Power
- {
- public static void main(String[] args)
- {
- // Read value for n
- Scanner in = new Scanner(System.in);
- double x = in.nextDouble();
- int n = in.nextInt();
- if(n == 0){System.out.println("1.0"); return;}
- System.out.println(power(x, n));
- }
- public static double power(double x, int n)
- {
- if(n == 1){return x;}
- return x * power(x, --n);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement