C Program For Tower of Hanoi Algorithm using Recursion

Learn How To Solve Tower of Hanoi Algorithm in C Programming Language. This C Program For Tower of Hanoi problem using Recursion method is explained below. The Tower of Hanoi Algorithm in Data Structures is a very common Interview Question for Beginners.

Tower of Hanoi Problem Explanation

Towers of Hanoi also known as Lucas’ Tower or Tower of Bramha’s is a mathematical puzzle developed by a Mathematician of French Origin named Édouard Lucas. It is believed that the Solution and Problem for Towers of Hanoi Algorithm was invented by the mathematician in an Indian city in 1883.

Image Source: MathForum

What is Tower of Hanoi Problem About?

Tower of Hanoi Algorithm is to move the Disks on the Source Tower to the Destination Tower. But, you should ensure that the Disks on the Destination Tower should be in the same format as in the Source Tower i.e., the Largest Disk should be at the Bottom Position and the Smallest Disk should be at the Top Position.

In other words, a Larger Disk should not be kept on Top of a Smaller Disk. To move these Disks, you can make use of a Temporary Tower , also called as an Auxiliary Tower. A Tower is also frequently called as Peg.

Also Read: C Program To Print Fibonacci Series using Recursion

Conditions For Towers of Hanoi Algorithm

Larger Disk cannot be placed on Smaller Disks

We can shift only One Disk at a time from One Tower to another.

What is Recursion?

Calling a Function again and again till your Condition is not met is basically Recursion. Recursive Programs tend to Execute a Single Function repeatedly along with its Terminating Condition. It is important to have a Terminating Condition for Recursive Calls so as to prevent from going into an Infinite Loop. Refer this Factorial Program in C Language to understand more about Recursion. We have used Recursion method to solve Towers of Hanoi Algorithm.

How Does Tower of Hanoi Algorithm Work?

The Tower of Hanoi Formula and the Steps For Moving N Disks from Source Tower to Destination Tower:

Move N-1 Disks from Source Tower To Temporary Tower Move Nth Disk from Source Tower To Destination Tower Move N-1 Disks from Temporary Tower To Destination Tower (using Source Tower as Temporary Tower)

For a total of n disks, 2n – 1 moves or disk shift are required.

Note: This code To Solve Towers of Hanoi Problem using Recursion in C Programming Language is developed in Linux Ubuntu Operating System and compiled with GCC Compiler.

Also Read: Tower of Hanoi without Recursion in C Programming

Code For Tower of Hanoi Algorithm in C Programming using Recursion

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include<stdio.h> int tower_of_hanoi ( int limit , char source_tower , char temporary_tower , char destination_tower ) { if ( limit == 1 ) { printf ( "

Move Disk %d From %c To %c

" , limit , source_tower , destination_tower ) ; return 0 ; } tower_of_hanoi ( limit - 1 , source_tower , destination_tower , temporary_tower ) ; printf ( "Move Disk %d From %c To %c

" , limit , source_tower , destination_tower ) ; tower_of_hanoi ( limit - 1 , temporary_tower , source_tower , destination_tower ) ; return 0 ; } int main ( ) { char source_tower = 'A' , temporary_tower = 'B' , destination_tower = 'C' ; int limit ; printf ( "

Enter The Number of Disks:\t" ) ; scanf ( "%d" , & limit ) ; printf ( "

Sequence of Disks:

" ) ; tower_of_hanoi ( limit , source_tower , temporary_tower , destination_tower ) ; printf ( "

" ) ; return 0 ; }

Also Read: C Program To Generate Floyd’s Triangle

Output

If you have any compilation errors or doubts in this Tower of Hanoi in C Programming Language, let us know about in the Comment Section below.