Tower of hanoi using rec

How to add T1 T2 T3 as it is showing overflow in char
#include
using namespace std;

void towerofhanoi(int n,char str,char dest,char help){
//base case
if(n==0){
return;
}

// recursive case

towerofhanoi(n-1,str,help,dest);

cout<<"Move "<<n<<"th"<<" disc from "<<str<<" to "<<dest<<endl;

towerofhanoi(n-1,help,dest,str);

}

int main(){
int n;
cin>>n;

towerofhanoi(n , 'A' , 'C' , 'B');

return 0;

}

hi @abhinavssr2003_eab0717682969e5c
Have a look at the corrected code–>

#include<iostream>
using namespace std;

void towerofhanoi(int n,string str,string dest,string help){
    //base case
    if(n==0){
        return;
    }

    // recursive case
    towerofhanoi(n-1,str,help,dest);

    cout<<"Move "<<n<<"th"<<" disc from "<<str<<" to "<<dest<<endl;

    towerofhanoi(n-1,help,dest,str);
}

int main(){
    int n;
    cin>>n;
    towerofhanoi(n , "T1" , "T2" , "T3");
    return 0;
}

how to add the number of steps??

hi @abhinavssr2003_eab0717682969e5c
you can do it like this–>

#include<iostream>
using namespace std;

void towerofhanoi(int n,string str,string dest,string help, int &cnt){
    //base case
    if(n==0){
        return;
    }

    cnt++;
    // recursive case
    towerofhanoi(n-1,str,help,dest,cnt);

    cout<<"Move "<<n<<"th"<<" disc from "<<str<<" to "<<dest<<endl;

    towerofhanoi(n-1,help,dest,str,cnt);
}

int main(){
    int n;
    cin>>n;
    int cnt = 0;
    towerofhanoi(n , "T1" , "T2" , "T3", cnt);
    cout<<cnt<<endl;
    return 0;
}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.