Friday 23 September 2016

Towers of Hanoi by recursion

/* Towers of Hanoi by recursion */

#include<stdio.h>
void toh(int,char,char,char);

int main()
{
int n=3;
toh(n,'A','B','C');
return 0;
}

void toh(int n,char a,char b,char c)
{
if(n==1)
printf("\nMoved from %c to %c",a,c);
else
{
toh(n-1,a,c,b);
toh(1,a,' ',c);
toh(n-1,b,a,c);
}
}

No comments:

Post a Comment

Featured post

The Thrill of Programming: Exploring the Satisfaction Behind the Code

Different individuals find satisfaction in programming for various reasons. Here are some common factors that tend to bring satisfaction to ...