Home / EG Comp Lab / C Language / Implement Tower of Hanoi using C language

Implement Tower of Hanoi using C language

This C program uses recursive function & solves the 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.
Here is the source code of the C program for solving towers of hanoi.
[message_box title=”PROGRAM” color=”yellow”]

#include<stdio.h>
void TOH(int num, char x, char y, char z);
int main() {
   int num;
   printf(“\nEnter number of plates:”);
   scanf(“%d”, &num);
   TOH(num 1, ‘A’, ‘B’, ‘C’);
   return (0);
}
void TOH(int num, char x, char y, char z) {
   if (num > 0) {
      TOH(num 1, x, z, y);
      printf(“\n%c -> %c”, x, y);
      TOH(num 1, z, y, x);
   }
}
[/message_box]
Following Image will explain you more about tower of hanoi :
mrtoindia

About Utpal

I am third year student of computer engineering in L.D COLLEGE OF ENGINEERING,Ahmedabad.

Check Also

Independent Directed Acyclic Graphs for Resilient Multipath Routing

ABSTRACT: In order to achieve resilient multipath routing, we introduce the concept of independent directed …

Leave a Reply

Be the First to Comment!

Notify of
wpDiscuz