Implement Tower of Hanoi using C language

0
3165
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