Loading

Tower of Hanoi

C - Program to Implement Problem of Tower of Hanoi

 

 #include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<time.h>

class tower
{
   public:
        int count;
        void toh(int n,char x,char y,char z);
};


int main()
{
  int n;
  char x,y,z;
  char k='y';
  tower t;
  x='A';
  y='B';
  z='C';

  do
  {
    clrscr();
    float toc;
    clock_t end,start;
    start=clock();
    cout<<"\n\t\t\t\t Tower Of Hanoi ";
    cout<<"\n\t\t\t\t----------------";
    cout<<"\n\n\t\tEnter No of Disks :: ";
    cin>>n;
    t.count=0;
    t.toh(n,x,y,z);
    cout<<"\n\n\t\tNo of Steps Performed :: "<<t.count;
    end=clock();
    toc=(end - start)/CLK_TCK;
    cout<<"\n\t\tTime Complexity :: "<<toc;
    cout<<"\n\n\t\t\tDo you want to continue?(y/n)";
    k=getche();
  }while(k=='y');
  return 0;
}

void tower::toh(int n,char x,char y,char z)
{
   if(n>=1)
   {
     toh(n-1,x,z,y);
     printf("\n\t\tMove  Disk %d from %c to %c",n,x,y);
     count++;
     toh(n-1,z,y,x);
   }
}

Click Here to Download Source Code with Executable Program

Online Blackjack
Online casino gambling to play conveniently offer over 100 top online casino games play slots poker online blackjack

Gestion Sav
Newwayservice est un excellent logiciel sav. (service après-vente)

Bookmark the permalink.

Leave a reply