public class power_i { // we would like to have a set of static methods that // compute powers such as x square, x cube etc... // square public static int pow2(int x) { return x * x; } // cube public static int pow3(int x) { return x * x * x; } // 4th power public static int pow4(int x) { return x * x * x * x; } // generalization: n-th power // (works for n >= 0) public static int pown(int x, int n) { int result = 1; while (n > 0) { result *= x; n--; } return result; } // test our methods -> nice :-) public static void main(String a[]) { System.out.println(" 7^2 = " + pow2(7)); System.out.println("10^3 = " + pow3(10)); System.out.println(" 2^4 = " + pow4(2)); System.out.println(" 2^5 = " + pown(2, 5)); } }