Home Segments Top Top Previous Next

447: Mainline

You can experiment with recursivePowerOf2 in a program such as this:

public class Demonstrate { 
 public static void main (String argv[]) { 
  System.out.print("2 to the 3rd power is "); 
  System.out.println(recursivePowerOf2(3)); 
  System.out.print("2 to the 10th power is "); 
  System.out.println(recursivePowerOf2(10)); 
 } 
 public static int recursivePowerOf2 (int n) { 
  if (n == 0) { 
   return 1; 
  } 
  else { 
   return 2 * recursivePowerOf2(n - 1); 
  } 
 } 
} 
--- Result --- 
2 to the 3rd power is 8 
2 to the 10th power is 1024