Skip to content

Instantly share code, notes, and snippets.

@shrawanx
Forked from salilkansal/towersofhanoi.cpp
Created May 7, 2016 14:20
Show Gist options
  • Save shrawanx/611ddfd0c2851ee88d3b595dde8734c5 to your computer and use it in GitHub Desktop.
Save shrawanx/611ddfd0c2851ee88d3b595dde8734c5 to your computer and use it in GitHub Desktop.
Towers of Hanoi
#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