C Program for Tower of Hanoi Problem Using Recursion

Here you will get C program for a tower of Hanoi issue utilizing recursion.

The Tower of Hanoi (additionally called the Tower of Brahma or Lucas’ Tower and some of the time pluralized)

is a numerical game or puzzle. It comprises of three poles, and various plates of various sizes which can slide onto any pole.

The puzzle begins with the circles in a slick stack in the climbing request of size on one bar, the littlest at the top,

in this way making a funnel-shaped shape as appeared in beneath picture.

The goal of the puzzle is to move the whole stack to another pole, complying with the accompanying straightforward principles:

Just each circle can be moved in turn.

Each move comprises of taking the upper circle from one of the stacks and setting it over another stack,

for example, a circle must be moved in the event that it is the highest plate on a stack.

No plate might be put over a littler circle.

With three plates, the puzzle can be fathomed in seven moves. The base number of moves required to understand

a Tower of Hanoi puzzle is 2n-1, where n is the number of circles.

Animated arrangement of the Tower of Hanoi Puzzle for T(4,3)

C Program utilizing recursion is given underneath which discovers the answer for Tower of Hanoi Problem.

C Program for Tower of Hanoi Problem Using Recursion

#include<stdio.h>
 
void TOH(int,char,char,char);
 
void main()
{
	int n;
	printf("How many plates?");
	scanf("%d",&n);
	TOH(n,'A','B','C');
}
 
void TOH(int n,char x,char y,char z)
{
	if(n>0)
	{
		TOH(n-1,x,z,y);
		printf("\n%c -> %c",x,y);
		TOH(n-1,z,y,x);
	}
}

Output

How many plates?3

A -> B
A -> C
B -> C
A -> B
C -> A
C -> B
A -> B

Remark beneath on the off chance that you have questions or discovered anything inaccurate identified with the above program for a tower of Hanoi in C.

Leave a Comment

error: Alert: Content is protected!!