import java.io.*; class PowerIt2 { 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) // Efficiently calculates n to the power of m iteratively { int pow=n, acc=1, count=m; while(count!=0) { if(count%2==1) acc=acc*pow; pow=pow*pow; count=count/2; } return acc; } }