Java Reference
In-Depth Information
Display 11.3
The Recursive Method power (part 2 of 2)
11 if (n < 0)
12 {
13 System.out.println("Illegal argument to power.");
14 System.exit(0);
15 }
16 if (n > 0)
17 return ( power(x, n - 1)*x );
18 else // n == 0
19 return (1);
20 }
21 }
Sample Dialogue
3 to the power 0 is 1
3 to the power 1 is 3
3 to the power 2 is 9
3 to the power 3 is 27
Display 11.4
Evaluating the Recursive Method Call power(2,3)
1
1
1 *2
power(2, 0) *2
is
1*2
2
2 *2
power(2, 1) *2
2*2 is 4
power(2, 2) *2
4 *2
4*2 is 8
power(2, 3)
8
Start Here
power(2, 3) is 8
 
 
Search WWH ::




Custom Search