Tower of Hanoi is mathematical puzzle/problem. The goal of the puzzle is to move all the disks from source to destination, adhering to the following rules: Rule-1: Move only one disk at a time. Rule-2: A larger disk may not be placed on the top of a smaller disk. Rule-3: The top disk of any stack can be moved to the top disk of any other stack, with the restriction that you can't move a larger disk on top of a smaller disk.
C program for the Tower of Hanoi
kw.c
#include<stdio.h>intmain(){intn;voidtowers(intn,charsource,chardestination,charauxiliary);printf("———————————————————————————————————————————");printf("\nProgram for the Tower of Hanoi");printf("\n———————————————————————————————————————————");printf("\nEnter Number of Disks ");scanf("%d",&n);towers(n,'A','B','C');printf("\n———————————————————————————————————————————\n");return0;}voidtowers(intn,charsource,chardestination,charauxiliary){if(n==1){printf("\nMove disk 1 from peg %c to peg %c",source,destination);return;}towers(n-1,source,auxiliary,destination);printf("\nMove disk %d from peg %c to peg %c",n,source,destination);towers(n-1,auxiliary,destination,source);}
Output
kodingwindow@kw:~$ gcc kw.c kodingwindow@kw:~$ ./a.out
———————————————————————————————————————————
Program for the Tower of Hanoi
———————————————————————————————————————————
Enter Number of Disks 3
Move disk 1 from peg A to peg B
Move disk 2 from peg A to peg C
Move disk 1 from peg B to peg C
Move disk 3 from peg A to peg B
Move disk 1 from peg C to peg A
Move disk 2 from peg C to peg B
Move disk 1 from peg A to peg B
———————————————————————————————————————————
kodingwindow@kw:~$
Dear User, Thank you for visitng Kodingwindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.
Dear User, We are grateful for your interest in the Kodingwindow Android app. We intend to soon make it available on the Google Play Store. Currently, Android versions 10 and up require manual apk installations. Would you like to get the app?