C++ Tower of Hanoi program

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

int move;
void movetower(int n,char from,char to,char temp);
void movedisk(char from,char to);
int main()
{
    int disks,moves;
    cout<<"\t\tWelcome to Tower of Hanoi Program : "<<endl;
    cout<<"\t\tEnter number of disks ";
    cin>>disks;
    cout<<endl;
    movetower(disks,'1','2','3');
    // to count total number of moves of disk
    moves=(pow(2,disks))-1;

    cout<<"\n\t\tTotal moves is = "<<moves<<endl;
    return 0;
}
void movetower(int n,char from,char to,char temp)
{
   if (n==1)
{


      movedisk(from,to);
}
      else
      {
         movetower(n-1,from,temp,to);
         movedisk(from,to);
         movetower(n-1,temp,to,from);
      }


}
void movedisk(char from,char to)
{

   cout<<"\t\tMove top disk from "<<from<<" to "<<to<<endl;

}

No comments:

Post a Comment