Tower of hanoi problem

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int x = towerOfHanoi(n,β€œT1”,β€œT2”,β€œT3”);
System.out.println(x);
}
public static int towerOfHanoi(int x , String source, String destination, String helper){
if(x == 0){
return count;
}
int count = 0;
towerOfHanoi(x-1,source, helper, destination);
System.out.println("Move "+x+"th disc from β€œ+source+” to "+ destination);
count++;
towerOfHanoi(x-1,helper, destination, source);
return count;
}
}===what’s the error please make corrections in my code

you just need to handle count variable carefully.
look into correct use of count variable.
the correct code is:

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int x = towerOfHanoi(n,β€œT1”,β€œT2”,β€œT3”);
System.out.println(x);
}
public static int towerOfHanoi(int x , String source, String destination, String helper){
if(x == 1){
System.out.println("Move "+1+"th disc from β€œ+source+” to "+ destination);
return 1;
}

int count = towerOfHanoi(x-1,source, helper, destination);
System.out.println("Move "+x+"th disc from β€œ+source+” to "+ destination);
count += towerOfHanoi(x-1,helper, destination, source);
return count+1;
}
}

Thanks.

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.