Tower of Hanoi C Program Assignment.
Tower of Hanoi C Program Assignment.
Tower of Hanoi C Program Assignment.
UNIT-2 ASSIGNMENT
(02)
FOR
DATA STRUCTURE
SUBMITTED BY:
ROBERT LOITONGBAM
SECOND YEAR(CSE)
ROLL NO.:40
Tower of Hanoi
DEFINITION:
Example:
Suppose no of disks are 4 and the starting rod is ‘A’, the auxiliary
rod is ‘B’ and the ending rod is ‘C’.
In each step first we move the top most node of peg ‘A’ to auxiliary
peg ‘B’ and then finally move to ‘C’ which is the desired peg.
Initially:
All disks are stack on peg ‘A’ with order of their size being Disk3 <
Disk2 < Disk1
Disc 3
Disc 2
Disc 1
Tower of Hanoi
Moving Disc 3 To Peg C:
Disc 2
Disc 1 Disc 3
Disc 3
Disc 1 Disc 2
Disc 3
Disc 2 Disc 1
Tower of Hanoi
Disc 3 Disc 2
Disc 1
Disc 3
Disc 2
Disc 1
Now this is the desired solution as all of the disks are stacked on
peg ‘C’.
Tower of Hanoi
PROGRAM:
#include<bits/stdc++.h>
void toh(int, char, char, char);
int main(){
char source = 'A', destination='B', Auxiliary='C';
int n;
cout<< "Enter value of n\t";
cin>> n;
toh(n, source, destination, Auxiliary);
return 0;
}
Output:
Conclusion: