C program to solve Tower of Hanoi problem for n disks using recursion.
Simple C program to solve Tower of Hanoi problem for n disks using recursion.
Code:
#include <stdio.h>#include<conio.h>
void tower(int n, char from, char to, char through){
if (n==1){
printf("\n Move disk 1 from rod %c to rod %c", from, to);
return;
}
tower(n-1, from, through, to);
printf("\n Move disk %d from rod %c to rod %c", n, from, to);
tower(n-1, through, to, from);
}
main(){
int n;
clrscr();
printf("Enter the number of disk: ");
scanf("%d",&n);
tower(n, 'A', 'C', 'B');
getch();
}
Comments
Post a Comment