0% found this document useful (0 votes)
10 views2 pages

TOWEROFHANIO

DSA program

Uploaded by

snehatomake05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

TOWEROFHANIO

DSA program

Uploaded by

snehatomake05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Name :-Sneha Prabhakar Tomake.

Class:-SY[AIML]
Roll No:-71
PRN NO:2425010004
 TOWER OF HANIO
#include <stdio.h>

// Recursive function to solve Tower of Hanoi problem


void towerOfHanoi(int n, char source, char destination, char auxiliary) {
if (n == 1) {
printf("Move disk 1 from %c to %c\n", source, destination);
return;
}
// Move n-1 disks from source to auxiliary
towerOfHanoi(n - 1, source, auxiliary, destination);

// Move the nth disk from source to destination


printf("Move disk %d from %c to %c\n", n, source, destination);

// Move n-1 disks from auxiliary to destination


towerOfHanoi(n - 1, auxiliary, destination, source);
}

int main() {
int n; // Number of disks

printf("Enter the number of disks: ");


scanf("%d", &n);

printf("The sequence of moves involved in the Tower of Hanoi are:\n");


towerOfHanoi(n, 'A', 'C', 'B'); // A = source, C = destination, B = auxiliary

return 0;
}
OUTPUT:
Enter the number of disks: 3
The sequence of moves involved in the Tower of Hanoi are:
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

You might also like