Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Power N
- Given base and n that are both 1 or more, compute recursively (no loops) the value of base to the n power, so powerN(3, 2) is 9 (3 squared).
- Input
- On the first line you will be given the base(b). On the second line you will be given the power(n).
- Output
- On the only output line you should print the result of b to the power of n.
- Constraints
- b>0, n>0
- Sample tests
- Input
- 3
- 1
- Output
- 3
- Input
- 3
- 2
- Output
- 9
- import java.util.Scanner;
- public class PowerN {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int h = Integer.parseInt(scanner.nextLine());
- int y = Integer.parseInt(scanner.nextLine());
- System.out.println(power(h,y));
- }
- public static int power(int h, int y){
- if(y==0){
- return 1;
- }
- return h * power(h, y-1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment