- 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

# C Program for Tower of Hanoi

The tower of Hanoi is a mathematical puzzle. It consists of three rods and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top. We have to obtain the same stack on the third rod.

The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules−

Only one disk can be moved at a time.

Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.

No disk may be placed on top of a smaller disk.

## Sample

**Input ** − 3

**Output ** − A to B

A to C

B to C

A to B

C to A

C to B

A to B Explanation − uses recursive function & solves the tower of Hanoi.

## Example

#include<stdio.h> void TOH(int n,char x,char y,char z) { if(n>0) { TOH(n-1,x,z,y); printf("

%c to %c",x,y); TOH(n-1,z,y,x); } } int main() { int n=3; TOH(n,'A','B','C'); }

## Output

A to B A to C B to C A to B C to A C to B A to B

- Related Articles
- Python Program for Tower of Hanoi
- Tower Of Hanoi Problem
- C++ program to Solve Tower of Hanoi Problem using Binary Value
- C Program for Program for array rotation?
- C Program for subtraction of matrices
- C Program for product of array
- C++ Program for Coefficient of variation
- Program for factorial of a number in C program
- C++ program for multiplication of array elements
- C++ Program for sum of arithmetic series
- Program for volume of Pyramid in C++
- Program for Area Of Square in C++
- C Program for Circumference of a Parallelogram
- C++ Program for Derivative of a Polynomial
- C++ Program for QuickSort?