A simple example of recursion : Class Method « Language Basics « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Collections Data Structure
3. Components
4. Database ADO.net
5. Development Class
6. Event
7. File Stream
8. GUI Windows Form
9. Language Basics
10. Network
11. Office
12. Regular Expressions
13. Services Event
14. Thread
15. Web Services
16. Windows
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C# / C Sharp » Language Basics » Class MethodScreenshots 
A simple example of recursion
A simple example of recursion

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// A simple example of recursion.  
 
using System; 
 
class Factorial {  
  // This is a recursive function.  
  public int factR(int n) {  
    int result;  
  
    if(n==1return 1;  
    result = factR(n-1* n;  
    return result;  
  }  
  
  // This is an iterative equivalent.  
  public int factI(int n) {  
    int t, result;  
  
    result = 1;  
    for(t=1; t <= n; t++result *= t;  
    return result;  
  }  
}  
  
public class Recursion {  
  public static void Main() {  
    Factorial f = new Factorial();  
  
    Console.WriteLine("Factorials using recursive method.");  
    Console.WriteLine("Factorial of 3 is " + f.factR(3));  
    Console.WriteLine("Factorial of 4 is " + f.factR(4));  
    Console.WriteLine("Factorial of 5 is " + f.factR(5));  
    Console.WriteLine();  
 
    Console.WriteLine("Factorials using iterative method.");  
    Console.WriteLine("Factorial of 3 is " + f.factI(3));  
    Console.WriteLine("Factorial of 4 is " + f.factI(4));  
    Console.WriteLine("Factorial of 5 is " + f.factI(5));  
  }  
}


           
       
Related examples in the same category
1. Define methods that return a value and accept parametersDefine methods that return a value and accept parameters
2. Class a class methodClass a class method
3. Call class methods 2Call class methods 2
4. Method overloading testMethod overloading test
5. Add a method to BuildingAdd a method to Building
6. A simple example that uses a parameterA simple example that uses a parameter
7. Add a method that takes two argumentsAdd a method that takes two arguments
8. Use a class factoryUse a class factory
9. Return an arrayReturn an array
10. Demonstrate method overloadingDemonstrate method overloading
11. Automatic type conversions can affect overloaded method resolutionAutomatic type conversions can affect 
   overloaded method resolution
12. Overloading ClassesOverloading Classes
13. C# Classes Member FunctionsC# Classes Member Functions
w_w__w.__j___a___va__2___s._co__m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.