Solving Tower of Hanoi problem with n disks using C program

Tower of Hanoi problem with n disk using c program and 3rd Sem Data Structure and Application Lab 4 b Program

Design, Develop and Implement a Program in C for the following Stack Applications
Solving Tower of Hanoi problem with n disks

/* Design, Develop and Implement a Program in C for the following Stack Applications
Solving Tower of Hanoi problem with n disks */


#include <stdio.h>

void towerOfHanoi(int n, char from, char to, char aux)
{
    if (n == 1)
    {
        printf("Move disk 1 from %c to %c\n", from, to);
        return;
    }
    towerOfHanoi(n - 1, from, aux, to);
    printf("Move disk %d from %c to %c\n", n, from, to);
    towerOfHanoi(n - 1, aux, to, from);
}

int main()
{
    int n;
    printf("Enter the number of disks: ");
    scanf("%d", &n);
    towerOfHanoi(n, 'A', 'C', 'B');
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *