Untitled

mail@pastecode.io avatar
unknown
java
a year ago
778 B
0
Indexable
Never

import java.util.*;

public class TowersOfHanoi
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of disks: ");
        int n = scanner.nextInt();
        towersOfHanoi(n, 'A', 'B', 'C');
        scanner.close();
    }

    public static void towersOfHanoi(int n, char source, char auxiliary, char destination)
    {
        if (n == 1)
        {
            System.out.println("Move disk 1 from " + source + " to " + destination);
            return;
        }
        towersOfHanoi(n - 1, source, destination, auxiliary);
        System.out.println("Move disk " + n + " from " + source + " to " + destination);
        towersOfHanoi(n - 1, auxiliary, source, destination);
    }
}