Chapter 6 Methods 217 5! 5 * 4! (Bulletproof web design)

Chapter 6 Methods 217 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 Final value = 120 5! = 5 * 24 = 120 is returned 4! = 4 * 6 = 24 is returned 3! = 3 * 2 = 6 is returned 2! = 2 * 1 = 2 is returned 1 returned (a) Procession of recursive calls (b) Values returned from each recursive call Fig. 6.14 Recursive evaluation of 5!. returns. If number is greater than 1, line 23 expresses the problem as the product of number and a recursive call to Factorial, evaluating the factorial of number -1. Note that Factorial( number -1 ) is a slightly simpler problem than the original calculation Factorial( number). 1 // Fig. 6.15: FactorialTest.cs 2 // Calculating factorials with recursion. 3 using System; 4 using System.Drawing; 5 using System.Collections; 6 using System.ComponentModel; 7 using System.Windows.Forms; 8 using System.Data; 9 10 public class FactorialTest : System.Windows.Forms.Form 11 { 12 private System.Windows.Forms.Button showFactorialsButton; 13 private System.Windows.Forms.Label outputLabel; 14 15 // Visual Studio .NET generated code 16 17 public long Factorial( long number ) 18 { 19 if ( number <= 1 ) // base case 20 return 1; 21 22 else 23 return number * Factorial( number - 1 ); 24 } 25 Fig. 6.15 Calculating factorials with a recursive method. (Part 1 of 2.)
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Leave a Reply