﻿ Sample Code

### Towers of Hanoi

//---------------------------------------------------------
// File: Towers of Hanoi 1.cpp
//
// Purpose: Solve the towers of Hanoi puzzle. The user
//   enters the number of disks.
//
// Author: D. Searls
//         Asbury College
//         April 1998
//---------------------------------------------------------

#include <iostream>

using namespace std;

//-----------------------------------------------
// Function: move
//
// Returns: void
//
// Purpose: move n disks from src peg to dst peg
// In Parameters: n, i, j
//-----------------------------------------------
void move(int n, int src, int dst)
{
int k;    // The third peg

if (n == 1)
{
cout << "Move a disk from peg " << src << " to peg " << dst << endl;
}
else
{
k = 6 - src - dst;
move(n-1, src, k);
move(1, src, dst);
move(n-1, k, dst);
}
}

//*********************************************************
//                  M A I N   D R I V E R
//*********************************************************

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

cout << "Towers of Hanoi" << endl << endl;
cout << "How many disks? ";
cin >> n;
cout << endl;
move(n, 1, 3);
cout << endl;

return 0;
}