Notification texts go here Contact Us Click here

Tower of Hanoi solve problem with program .

Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated






Tower of Hanoi  : 

The Tower of Hanoi is a classic mathematical puzzle or problem that involves moving a tower of disks from one peg (rod) to another while following specific rules. It is a popular example used in computer science and recursion studies. The objective is to move all the disks from the source peg to the destination peg, using an auxiliary peg if necessary, while never placing a larger disk on top of a smaller disk.


The problem is defined as follows:


1. There are three pegs, usually named "A," "B," and "C."

2. There are n disks of different sizes placed on peg A, with the largest disk at the bottom and the smallest at the top.

3. The task is to move all the disks from peg A to peg C, using peg B as an auxiliary peg, following the rules below:

   a. Only one disk can be moved at a time.

   b. A disk can only be placed on top of a larger disk or an empty peg.


Here's an example solution for a Tower of Hanoi problem with three disks:


Step 1: Move the smallest disk from A to C.

Step 2: Move the medium disk from A to B.

Step 3: Move the smallest disk from C to B.

Step 4: Move the largest disk from A to C.

Step 5: Move the smallest disk from B to A.

Step 6: Move the medium disk from B to C.

Step 7: Move the smallest disk from A to C.


The number of moves required to solve the Tower of Hanoi puzzle with n disks is 2^n - 1. The problem becomes increasingly challenging as the number of disks increases.


The Tower of Hanoi problem can be efficiently solved using recursion. The recursive algorithm follows the steps below:


1. Base Case: If there is only one disk, move it directly from the source peg to the destination peg.

2. Recursive Step: For n disks, follow these steps:

   a. Move n-1 disks from the source peg to the auxiliary peg, using the destination peg.

   b. Move the nth disk (the largest) from the source peg to the destination peg.

   c. Move the n-1 disks from the auxiliary peg to the destination peg, using the source peg.


This algorithm will solve the Tower of Hanoi problem for any number of disks efficiently.


  Program of Tower of Hanoi using C language .

  1. Sure! Below is an implementation of the Tower of Hanoi problem in C using a recursive function.
  2.  
  3. ```c
  4. #include <stdio.h>
  5.  
  6. void towerOfHanoi(int n, char source, char auxiliary, char destination) {
  7. if (n == 1) {
  8. printf("Move disk 1 from %c to %c\n", source, destination);
  9. return;
  10. }
  11.  
  12. towerOfHanoi(n - 1, source, destination, auxiliary);
  13. printf("Move disk %d from %c to %c\n", n, source, destination);
  14. towerOfHanoi(n - 1, auxiliary, source, destination);
  15. }
  16.  
  17. int main() {
  18. int numDisks;
  19. printf("Enter the number of disks: ");
  20. scanf("%d", &numDisks);
  21.  
  22. // Assuming the pegs are named A, B, and C
  23. char source = 'A';
  24. char auxiliary = 'B';
  25. char destination = 'C';
  26.  
  27. towerOfHanoi(numDisks, source, auxiliary, destination);
  28.  
  29. return 0;
  30. }
  31. ```
  32.  
  33. In this program, the `towerOfHanoi` function is a recursive function that takes four arguments:
  34.  
  35. 1. `n`: The number of disks to move.
  36. 2. `source`: The source peg from where the disks need to be moved.
  37. 3. `auxiliary`: The auxiliary peg used to facilitate the movement.
  38. 4. `destination`: The destination peg where the disks need to be placed.
  39.  
  40.  
  41.  
  42. Remember that the number of moves required to solve the Tower of Hanoi problem for `n` disks
  43. is `2^n - 1`.

  Program of Tower of Hanoi using Python language .

  1. Certainly! Here's the Python implementation of the Tower of Hanoi problem using recursion:
  2.  
  3. ```python
  4. def tower_of_hanoi(n, source, auxiliary, destination):
  5. if n == 1:
  6. print(f"Move disk 1 from {source} to {destination}")
  7. return
  8.  
  9. tower_of_hanoi(n - 1, source, destination, auxiliary)
  10. print(f"Move disk {n} from {source} to {destination}")
  11. tower_of_hanoi(n - 1, auxiliary, source, destination)
  12.  
  13. if __name__ == "__main__":
  14. num_disks = int(input("Enter the number of disks: "))
  15.  
  16. # Assuming the pegs are named A, B, and C
  17. source_peg = 'A'
  18. auxiliary_peg = 'B'
  19. destination_peg = 'C'
  20.  
  21. tower_of_hanoi(num_disks, source_peg, auxiliary_peg, destination_peg)
  22. ```
  23.  
  24. In this Python code, the `tower_of_hanoi` function is a recursive function
  25. that performs the steps to move `n` disks from the source peg to
  26. the destination peg using the auxiliary peg, following the rules of the
  27. Tower of Hanoi problem.
  28.  
  29. When you run this code, it will prompt you to enter
  30. the number of disks you want to solve the problem for.
  31. Then, it will print the step-by-step instructions for moving
  32. the disks to solve the Tower of Hanoi problem.
  33. The number of moves required to solve
  34. the Tower of Hanoi problem for `n` disks is still `2^n - 1`,
  35. just like in the C implementation.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.