Implement Tower of Hanoi using C language

0
2695
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
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments