// CSE 143, Autumn 2013 // This test program checks the functionality of our pow method. public class PowerTest { public static void main(String[] args) { System.out.println(pow(3, 4)); // 81 System.out.println(pow(2, 8)); // 256 System.out.println(pow(10, 5)); // 100000 System.out.println(pow(-4, 5)); // -1024 System.out.println(pow(2, 30)); // 1073741824 } // Returns (base) raised to the (exponent)th power, recursively. // Precondition: exponent >= 0 public static int pow(int base, int exponent) { if (exponent == 0 || base == 1) { return 1; } else if (exponent % 2 == 0) { return pow(base * base, exponent / 2); } else { return base * pow(base, exponent - 1); } } }