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

Program to illustrate the concept of templates.

To create an html file to implement the concept of margin, padding using cascading style sheets.

To configure the IP address for a computer connected to LAN