Introduction to Tower of Hanoi

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>
int main()
{
    int n;
    void towers(int n, char source, char destination, char auxiliary);
    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");
    return 0;
}
void towers(int n, char source, char destination, char auxiliary)
{
    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:~$
Advertisement