Created
February 15, 2015 15:20
-
-
Save salilkansal/e79b6feb672fd1d16034 to your computer and use it in GitHub Desktop.
Towers of Hanoi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<conio.h> | |
using namespace std; | |
void tower(int, char[], char[], char[]); | |
void main() | |
{ | |
int N; | |
cout << "Enter the number of disks to be transferred: "; | |
cin >> N; | |
if (N < 1) | |
{ | |
cout << "\nIncorrect Value"; | |
exit(0); | |
} | |
else | |
{ | |
cout << "\nThe following moves are required for N= " << N<<endl; | |
tower(N, "BEG", "AUX", "END"); | |
} | |
_getch(); | |
} | |
void tower(int NUM, char A[5], char B[5], char C[5]) | |
{ | |
if (NUM == 1) | |
{ | |
cout << A << "->" << C<<"\t"; | |
return; | |
} | |
tower(NUM - 1, A, C, B); | |
cout << A << "->" << C << "\t"; | |
tower(NUM - 1, B, A, C); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment