Tower Of Hanoi (TOH) #simple code 🫨🫨👍
code is written and implemented by :
Himanshu Tiwari
Play Game to learn concept(click 👉) : tower of hanoi GAME
#include<stdio.h>
//s= source of disks
//h= helping tower
//d=destination tower
void tower_steps(int n,char s,char h,char d){
if(n==0) return;
tower_steps(n-1,s,d,h);
printf("%c -> %c\n",s,d);
tower_steps(n-1,h,s,d);
return;
}
int count_steps(int a,int n){
a=2;
if(n==1) return a;
return a*count_steps(a,n-1);
}
int main(){
int n;
printf("enter no. of disks : ");
scanf("%d",&n);
int x=count_steps(2,n);
printf("steps = %d\n",x-1);
tower_steps(n,'A','B','C');
return 0;
}
output :
.png)
.png)
Comments
Post a Comment