Friday, September 6, 2013

Learn C : Recursive algorithm - start 1

recursive algorithm is the method used to solve common problems, to its algorithm is quite simple, the most important issue is how to fix the duration of the algorithm.
Let's look at some examples of application of recursive algorithms:

 
example 1: Resursive algorithm factorial.
 
 int Factorial( int n){
     If(n < 0) return -1;
     If( n == 0 ) return 1;
     return Factorial(n - 1)*n;


example 2: calculate F = x^y (x,y >=0)

Int CalculateFx(int x, int y){
     if(x<=0 || y <=0 ) return -1;
     if(x==0) return 0;
     if(y==0) return 1;
     return CalculateFx(x,y-1)*x;
}

No comments:

Post a Comment

Popular Posts