Towerof Hanoi
Towerof Hanoi
Esha Gupta
Associat e Senior Execut ive
Updated on Apr 15, 2024 13:12 IST
Tower of Hanoi Puzzle is a brainteaser that involves moving a stack of discs
between three pegs, adhering to specific constraints. Let us read more
about it!
The Tower of Hanoi is a classic mathematical puzzle. It consists of three rods (or
pegs) and several disks which are of different sizes, which can slide onto any rod.
The puzzle starts with the disks that are neatly stacked in ascending order of size
on one rod, the smallest disk on top, thus forming a conical shape. Here, we will see
a Tower of Hanoi Program in C.
Objective :
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 16 -Apr-20 24.
The goal is to move the entire stack to another rod, following these very basic rules
given below:
Source: Mathspp
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 16 -Apr-20 24.
Copy code
int main()
{
int n = 4 ; // Setting the count of disks
towerOfHanoi(n, 'A', 'C', 'B'); // Here, A, B, and C represent the pegs' identifiers
return 0;
}
Output :
Shif t disk 1 f rom peg A to peg B
Shif t disk 2 f rom peg A to peg C
Shif t disk 1 f rom peg B to peg C
Shif t disk 3 f rom peg A to peg B
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 16 -Apr-20 24.
Shif t disk 1 f rom peg C to peg A
Shif t disk 2 f rom peg C to peg B
Shif t disk 1 f rom peg A to peg B
Shif t disk 4 f rom peg A to peg C
Shif t disk 1 f rom peg B to peg C
Shif t disk 2 f rom peg B to peg A
Shif t disk 1 f rom peg C to peg A
Shif t disk 3 f rom peg B to peg C
Shif t disk 1 f rom peg A to peg B
Shif t disk 2 f rom peg A to peg C
Shif t disk 1 f rom peg B to peg C
The Tower of Hanoi puzzle with n disks can be solved in a minimum of 2 n−1 steps.
This presentation shows that a puzzle with 4 disks has taken 2 4 – 1 = 15 steps.
The output illustrates the solution to the Tower of Hanoi puzzle with 4 disks.
Starting with all disks on peg A, the moves sequentially shift disks between pegs,
ensuring a larger disk never lands on a smaller one. The steps gradually relocate the
disks, with the largest disk (disk 4) reaching its target peg (peg C) midway through.
The subsequent moves rearrange the smaller disks around it until all disks are
stacked on peg C in their original order.
C programming examples
If yo u want to learn C language by executing pro gramming examples. Then this
article o ffers yo u 17C pro gramming examples with o utputs, so that yo u can
learn fast.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 16 -Apr-20 24.
St ruct ure in C Programming: How t o Creat e and Use…
Programs
This article is a co mplete guide to understanding the meaning o f C structure and
by the end o f this article yo u will understand ho w to use and when to use...re ad m o re
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 16 -Apr-20 24.
Top 10 Pat t ern Programs in C
Have yo u ever wo ndered ho w intricate patterns can be created with simple
lo o ps in C? Pattern pro grams in C are a fascinating way to learn abo ut nested
lo o ps and co ntro l...re ad m o re
Thus, the Tower of Hanoi program in C showcases the power of recursion to solve
problems that might initially seem complex. This centuries-old puzzle demonstrates
how breaking a problem down into simpler instances of itself can lead to an elegant
solution. The C implementation effectively translates the iterative nature of the
puzzle into clear code steps. By understanding this algorithm, one gains insights into
the recursive problem-solving approach and appreciates the efficiency and simplicity
with which C handles such challenges.
FAQs
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 16 -Apr-20 24.