hey, it’s been always nice to see you when you are looking out for something to learn. it’s awesome. In this post, we will learn about the Tower of Hanoi Puzzle. We will learn how to solve this puzzle in the java programming language. We will use Java Recursive Function to Solve this. So let’s learn Java Recursive Program to Solve the Tower Of Hanoi Puzzle.

## What is the Tower Of Hanoi?

The Tower of Hanoi is a very famous mathematical game or puzzle. This puzzle was invented in 1883 by the French mathematician Γdouard Lucas. The puzzle can be played with any number of disks. The minimum number of moves required to solve the puzzle is 2^{n} – 1. So if we start to play with 3 disks our minimum move will be 7.

#### To play this game or to solve this puzzle you need to obey the following rules.

- Only one disk can be moved at a time.
- Each move consists of taking the upper disk from one of the rods and place it on top of another rod.
- You Can’t Place a larger disk on top of a smaller disk.

## Solving Tower Of Hanoi Puzzle [ Video ]

You can watch this video and see how I am solving this puzzle. You will have a clear picture of the game or puzzle.

Now Let me share the java program with you where we solved the puzzle with a recursive function.

## Java Program To Solve the Tower Of Hanoi Puzzle

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.practicehouse; /** * * @author arifur.rahman */ public class TowerOfHanoi { public static void main(String[] args) { int nDisks = 3; doTowers(nDisks, 'A', 'B', 'C'); } public static void doTowers(int topN, char from, char inter, char to) { if (topN == 1) { System.out.println("Disk 1 from "+ from + " to " + to); } else { doTowers(topN - 1, from, to, inter); System.out.println("Disk " + topN + " from " + from + " to " + to); doTowers(topN - 1, inter, from, to); } } } |

## Code Output From A Java Program To Solve the Tower Of Hanoi Puzzle

1 2 3 4 5 6 7 |
Disk 1 from poll A to poll C Disk 2 from poll A to poll B Disk 1 from poll C to poll B Disk 3 from poll A to poll C Disk 1 from poll B to poll A Disk 2 from poll B to poll C Disk 1 from poll A to poll C |

Congratulations, Wow today you have learned how to write a java recursive program to solve the Tower of Hanoi puzzle.

Keep Visiting us to learn more programming related problems that we all should practice to level up our skills. Happy Coding…

Clone / Download the Sample Project from GitHub : Download / Clone Sample Project

**Java – PHP – Python – Dart – Flutter – MySql – Spring – Hibernate – JavaScript – jQuery – BootStrap 4 – CSS – HTML**

I love to explore new technologies. If you like my tutorials please share your thoughts about my work.

Check out my YouTube Channel For Video Tutorials.

To Hire Me Please Contact Me Through Social Media or https:www.amialif.com

If you want to help others by doing donation please Help Them.

You also can connect with me via social media. Thanks

Great content! Super high-quality! Keep it up! π