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 create an html page with frames and frameset

1. Write a program to insert a new element at end as well as at a given position in an array in Java

1. To familiarize with network devices like switch, hub, routers and bridges in LaTex