Stats

Tuesday, 13 May 2014

                           TOWER OF HANOI C++ CODE

#include <iostream>
#include <conio.h>
using namespace std;

void tower(int a, char from, char aux, char to)
{
if (a == 1)
{
cout << "Move disc 1 from " << from << " to " << to << "\n";
return;
}
else
{
tower(a - 1, from, to, aux);
cout << "Move disc " << a << " from " << from << " to " << to << "\n";
tower(a - 1, aux, from, to);
}
}

void main()
{
int n;
cout << "Tower of Hanoi\n";
cout << "Enter number of discs : ";
cin >> n;
cout << "\n";
tower(n, 'A', 'B', 'C');
getch();
}

OUTPUT
Tower of Hanoi
Enter number of discs : 3

Move disc 1 from A to C
Move disc 2 from A to B
Move disc 1 from C to B
Move disc 3 from A to C
Move disc 1 from B to A
Move disc 2 from B to C

Move disc 1 from A to C

1 comment:

  1. Hey men I had a problem in this part:
    void main()
    and the program show me this:
    20 10 C:\Users\Alego\Desktop\Torre hanoi.cpp [Error] expected unqualified-id before ')' token

    But, if i was change Main fot int, again the program show me an error:
    20 10 C:\Users\Alego\Desktop\Torre hanoi.cpp [Error] expected unqualified-id before ')' token

    ReplyDelete