Recursive methods

In this chapter you will learn:

  1. How to write recursive methods
  2. Display a string in reverse by using recursion
  3. How to calculate fibonacci value in C#

Create recursive methods

A method can call itself is called recursion.

using System; /*from  j a  va 2 s  .c o  m*/
 
class Factorial {  
  // This is a recursive function.  
  public int factR(int n) {  
    int result;  
  
    if(n==1) return 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;  
  }  
}  
  
class MainClass {  
  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));  
  }  
}

The code above generates the following result.

Reverse a string

using System; //from ja  v  a  2  s . c o  m
  
class RevStr { 
 
  public void displayRev(string str) { 
    if(str.Length > 0)  
      displayRev(str.Substring(1, str.Length-1)); 
    else  
      return; 
 
    Console.Write(str[0]); 
  } 
} 
 
class MainClass { 
  public static void Main() {   
    string s = "this is a test"; 
    RevStr rsOb = new RevStr(); 
 
    Console.WriteLine("Original string: " + s); 
 
    Console.Write("Reversed string: "); 
    rsOb.displayRev(s); 
 
    Console.WriteLine(); 
  } 
}

The code above generates the following result.

fibonacci in C#

using System;//from j  a  v a  2 s.com
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;

public class MainClass
{
   public static void Main(){
      Console.WriteLine(Fibonacci(10));
   }
    static int Fibonacci(int x)
    {
        if (x <= 1)
            return 1;
        return Fibonacci(x - 1) + Fibonacci(x - 2);
    }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. What is polymorphism and how to use it
Home » C# Tutorial » Class
Classes and Structs
Encapsulation
Class and Struct Accessibility
Members
Static Members
Class
Class instance variables
Class instance Methods
Class Inheritance
Class constructor
Default constructors
Parameters of constructors
Constructor overloading
Static Constructor
Destructor
Override destructor
new operator
Class field
Static Fields
Read only field
Field default value
const value
Abstract class
Static Class
Partial type
Methods
Method local variable
Method Overloading
Method overloading with ref and out
Type conversion and method overloading
Error in Method overloading
virtual methods
Override methods
Method Parameter
Pass parameters
ref parameters
out parameters
Variable parameter length
Optional parameters
Named parameters
static method
function Return
Recursive methods
Virtual methods and polymorphism
Cast
Shadow inherited members
Seal a member
this keyword
base keyword
base and name hiding
Access Specifiers
private
public
protected
internal
Indexer
String type indexer
Multidimensional indexer
Indexer overloading
Indexer to append
Indexer logic
Properties
Properties getter and setter
Accessibilities of properties
Automatic properties
Read only and write only property
Virtual properties
Abstract Properties
Static Properties
interface
interface implementation
Explicit interface implementation
Interface inheritance
Indexer in an interface
Interface Properties
struct and interface
virtual interface implementation
as operator
is operator
null reference
Nested classes
object class
ToString method
GetHashCode
Object initialization
Extension method
Struct Instances vs. Class Instances