Write a program to solve the Tower of Hanoi puzzle.
Sample Input
input=3
Sample output=
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
code👇👇👇👇👇
#include<stdio.h>
void tower(int n,char beg,char aux,char end){
if(n==1){
printf("Move disk 1 from %c to %c\n",beg,end);
return;
}
tower(n-1,beg,end,aux);
printf("Move disk %d from %c to %c\n",n,beg,end);
tower(n-1,aux,beg,end);
}
void main(){
int n;
char A,B,C;
scanf("%d",&n);
A='A';
B='B';
C='C';
tower(n,A,B,C);
}
No comments:
Post a Comment