Ask Question
12 November, 01:14

Change the Towers of Hanoi program so that it does the following: a) Counts the number of ring moves and prints that - instead of the sequence of the moves. Use a static variable count of type int to hold the number of moves. b) Repeatedly prompts the user for the number of rings and reports the results, until the user enters a number less than 0

+3
Answers (1)
  1. 12 November, 01:40
    0
    Following are the program in the Java Programming Language.

    //import scanner class package

    import java. util. Scanner;

    //define class

    public class Tower_of_Hanoi {

    //define static integer variable

    public static int count = 0;

    //define function

    public static void Permute_Arrange (int n, char x, char ax, char to) {

    //set if statement

    if (n = = 1) {

    //increament in count by 1

    ++count;

    }

    //otherwise

    else

    {

    Permute_Arrange (n - 1, x, to, ax);

    ++count;

    Permute_Arrange (n - 1, ax, x, to);

    }

    }

    //define main function

    public static void main (String[] args)

    {

    //set scanner type object

    Scanner sc = new Scanner (System. in);

    //print message

    System. out. println ("Enter less than 0 to exit");

    //set the while infinite loop

    while (true)

    {

    //print message

    System. out. print ("Enter the number of Disks: ");

    //get input from the user

    int num_of_disk = sc. nextInt ();

    //set the if statement to break the loop

    if (num_of_disk<0)

    {

    //exit from the loop

    System. exit (0);

    }

    //call the function

    Permute_Arrange (num_of_disk, 'A', 'B', 'C');

    //print message with output

    System. out. println ("Total number of Disc Moves is: " + count);

    count = 0;

    }

    }

    }

    Output:

    Enter less than 0 to exit

    Enter the number of Disks: 4

    Total number of Disc Moves is: 15

    Enter the number of Disks: 7

    Total number of Disc Moves is: 127

    Enter the number of Disks: - 1

    Explanation:

    Here, we define a class named "Tower_of_Hanoi"

    Set the integer data type static variable "count" and initialize the value to 0. Define void data type static function "Permute_Arrange" and pass three characters type arguments "x", "ax", and to and one integer type argument "n", inside it we set if condition to increment in the count variable otherwise call the function. Finally, we define the main function to get input from the user and pass the argument list in function and call the function.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Change the Towers of Hanoi program so that it does the following: a) Counts the number of ring moves and prints that - instead of the ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers