sunfresh

Power N

Jan 20th, 2023
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. Power N
  2. 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).
  3.  
  4. Input
  5. On the first line you will be given the base(b). On the second line you will be given the power(n).
  6.  
  7. Output
  8. On the only output line you should print the result of b to the power of n.
  9.  
  10. Constraints
  11. b>0, n>0
  12.  
  13. Sample tests
  14. Input
  15. 3
  16. 1
  17. Output
  18. 3
  19. Input
  20. 3
  21. 2
  22. Output
  23. 9
  24.  
  25. import java.util.Scanner;
  26.  
  27. public class PowerN {
  28. public static void main(String[] args) {
  29. Scanner scanner = new Scanner(System.in);
  30.  
  31. int h = Integer.parseInt(scanner.nextLine());
  32. int y = Integer.parseInt(scanner.nextLine());
  33.  
  34. System.out.println(power(h,y));
  35. }
  36.  
  37. public static int power(int h, int y){
  38. if(y==0){
  39. return 1;
  40. }
  41. return h * power(h, y-1);
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment