can you please tell me what is problem in my code because it is showing wrong answer
import java.util.*;
public class Main{
public static void TOH(int n,char beg ,char aux ,char end){
if(n>=1){
TOH(n-1,beg,end,aux);
System.out.println("Moving ring "+ n + " from “+beg+” to "+end);
TOH(n-1,aux,end,beg);
}}
public static void main(String args[]){try{
Scanner sc=new Scanner(System.in);
nt n=sc.nextInt();
TOH(n,‘A’,‘B’,‘C’);}
catch(Exception e){System.out.println(e);}
}}
Tower of hanoi problem
there is mistake
pls make a dry run for the input 3 by yourself first
because this is a very interesting problem of recursion
once you find it by yourself it will help you in imagination of many recursion problem
after this
again if u are suffering
pls dont hessitate to ask doubts
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.
1 Like