import java.io.*; class PowerRec2a { public static void main(String[] args) throws IOException { int n,m; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter number n : "); n = Integer.parseInt(in.readLine()); System.out.print("Enter number m : "); m = Integer.parseInt(in.readLine()); System.out.print(n+" to the power of "+m+" is: "); System.out.println(power(n,m)); } public static int power(int n, int m) // The efficient recursive way of calculating n to the power of m // made inefficient by not saving and re-using the result of the // recursive call { if(m==0) return 1; else { int m2=m/2; if(m%2==0) return power(n,m2)*power(n,m2); else return power(n,m2)*power(n,m2)*n; } } }