- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Tower Of Hanoi Problemn
Tower of Hanoi is a puzzle problem. Where we have three stands and n discs. Initially, Discs are placed in the first stand. We have to place discs into the third or destination stand, the second or auxiliary stand can be used as a helping stand.
- But there are some rules to follow.
- We can transfer only one disc for each movement.
- Only the topmost disc can be picked up from a stand.
- No bigger disc will be placed at the top of the smaller disc.
This problem can be solved easily by recursion. At first, using recursion the top (n-1) discs are placed from source to auxiliary stand, then place the last disc from source to destination, then again place (n-1) disc from auxiliary stand to destination stand by recursion.
Input and Output
Input: Number of discs: 3 Output: 1. Move disk 1 from A to C 2. Move disk 2 from A to B 3. Move disk 1 from C to B 4. Move disk 3 from A to C 5. Move disk 1 from B to A 6. Move disk 2 from B to C 7. Move disk 1 from A to C
Algorithm
toh(n, s, a, d)
Input: Number of discs, source, auxiliary, destination.
Output: Steps to move discs from source to destination maintaining proper rules.
Begin if n = 1, then display move disc from s to d toh(n-1, s, d, a) display move disc from s to d toh(n-1, a, s, d) End
Example
#include<iostream> using namespace std; void TOH(int n, char s, char a, char d) { static int count = 0; //store number of counts if(n == 1) { count++; cout << count<< ". Move disk " << n << " from "<<s <<" to "<<d<<endl; return; //base case, when only one disk } TOH(n-1, s, d, a); //recursive call the function count++; cout << count<< ". Move disk " << n << " from "<<s <<" to"<<d<<endl; TOH(n-1, a, s, d); } int main() { int n; cout << "Enter the number of disks: "; cin >> n; TOH(n, 'A','B','C'); }
Output
Enter the number of disks: 3 1. Move disk 1 from A to C 2. Move disk 2 from A to B 3. Move disk 1 from C to B 4. Move disk 3 from A to C 5. Move disk 1 from B to A 6. Move disk 2 from B to C 7. Move disk 1 from A to C
- Related Articles
- Tower Of Hanoi Problem
- C++ program to Solve Tower of Hanoi Problem using Binary Value
- C Program for Tower of Hanoi
- Python Program for Tower of Hanoi
- N Queen Problem
- C++ Program to Solve N-Queen Problem
- Minimum Number of Platforms Problem
- Closest Pair of Points Problem
- Minimum Number of Jumps Problem
- A tower stands vertically on the ground. From a point on the ground, ( 20 mathrm{~m} ) away from the foot of the tower, the angle of elevation of the top of the tower is ( 60^{circ} ). What is the height of the tower?
- The angle of elevation of a tower from a point on the same level as the foot of the tower is ( 30^{circ} ). On advancing 150 metres towards the foot of the tower, the angle of elevation of the tower becomes ( 60^{circ} ). Show that the height of the tower is ( 129.9 ) metres (Use ( sqrt{3}=1.732 ) ).
- Partition problem
- Peterson’s Problem
- A person observed the angle of elevation of the top of a tower as ( 30^{circ} ). He walked ( 50 mathrm{~m} ) towards the foot of the tower along level ground and found the angle of elevation of the top of the tower as ( 60^{circ} ). Find the height of the tower.
- The angle of elevation of the top of a tower from a point on the ground, which is $30 m$ away from the foot of the tower is $30^o$. Find the height of the tower.
- Critical Section Problem

Advertisements