article

recursive functions

Email
Submitted on: 1/2/2015 7:22:00 PM
By: Callens Bob (from psc cd)  
Level: Beginner
User Rating: By 2 Users
Compatibility: C, C++ (general)
Views: 1929
 
     Some functions to show the use of recursion in programming. This contains Fibonacci,Towers of Hanoi,Ackermann function

 
				//Towers of Hanoi

//example: you have 4 disks and have to move all disks from 1 to 3. solve(4,1,3,2);
void solve(int disks, int src,int dest, int stack)
{
if(disks==1)
{
printf("Move from %d to %d\n",src,dest);
}
else {

solve(disks-1,src,stack,dest);
solve(1,src,dest,stack);
solve(disks-1,stack,dest,src);
}
}
//Fibonacci numbers
int fab(int n)
{
if(n==1 || n==2)
{
return 1;
}
else
return (fab(n-1)+fab(n-2));
}
//ackermann function
int ack(int m,int n)
{
if(m==0)
{
return n+1;
}

if(n==0)
{
return ack(m-1,1);
}
else
return ack(m-1,ack(m,n-1));
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.