Below is C program to Towers of Hanoi by recursion .
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /* 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