1. You are given a number x.
2. You are given another number n.Input Format
3. You are required to calculate x raised to the power n. Don't change the signature of power function .
A number xOutput Format
A number n
x raised to the power n
Constraints
1 <= x <= 10
0 <= n <= 9
Sample Input
2
5
Sample Output
32
Solution:
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
// write your code here
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int n = sc.nextInt();
System.out.println(power(x,n));
}
public static int power(int x, int n){
if(n == 0) return 1;
return x * power(x,n-1);
}
}
Comments
Post a Comment