Write a recursive function for Tower of Hanoi problem.

#include<iostream>
using namespace std;
int main()
{
int n;
void cal(int,char,char,char);
cout<<"How many disks?";
cin>>n;
cal(n,'A','B','C');
cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/";
}
void cal(int n,char source,char inter,char dest)
{
if(n==1)
cout<<"Disk 1 from "<<source<<" to "<<dest<<endl;
else
{
cal(n-1,source,dest,inter);
cout<<"Disk "<<n<<" from "<<source<<" to "<<dest<<endl;
cal(n-1,source,dest,inter);
}
}

Output:



Comments

Popular posts from this blog

To implement the various components of HTML5 Canvas

Program to illustrate the concept of templates.

Program to illustrate the order of execution of constructors and destructors in inheritance.