0% found this document useful (0 votes)
14 views6 pages

Towerof Hanoi

Tower of Hanoi

Uploaded by

Bhanu Flery
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Towerof Hanoi

Tower of Hanoi

Uploaded by

Bhanu Flery
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Solving Tower of Hanoi Program in C

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.

Explore Online C Programming Courses

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:

Only one disk can be moved at a time.


Each move consists of taking the upper disk f rom one of the stacks
and placing it on top of the other.
No disk may be placed on top of a smaller disk.

Source: Mathspp

Let’s Write a Simple C Program for Solving this Puzzle :

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

#include < stdio.h>

// Recursive C function to demonstrate the solution of the Tower of Hanoi problem


void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)
{
if (n == 1)
{
printf("\n Shift disk 1 from peg %c to peg %c", from_rod, to_rod);
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
printf("\n Shift disk %d from peg %c to peg %c", n, from_rod, to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}

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.

Thus, our puzzle is solved.

Learn Dat a Types in C Programming Wit h Examples


Data types are the type o f data sto red in a C pro gram. Data types are used while
defining a variable o r functio ns in C. It’s impo rtant fo r the co mpiler to ...re ad
m o re

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

C programming Keywords: A list and Explanat ion of …


Usage
Keywo rds in C refer to a set o f reserved wo rds with predefined meanings that
are used to write pro grams in the C pro gramming language. These keywo rds canno t be used as...re ad
m o re

Funct ion in C | About , Types and Examples


Have yo u heard o f functio ns in the C pro gramming language? Let's explo re it,
including its types and examples in detail! A functio n in C is a co llectio n o f
statements designed...re ad m o re

Relat ional Operat ors in C | About and Types


Relatio nal o perato rs in C, such as “less than”, “greater than”, “less than o r
equal to ”, “greater than o r equal to ”, “equal to ”, and “no t equal to ”, play a pivo tal
ro le...re ad m o re

Addit ion of Two Numbers in C


Delve into C pro gramming with a fo undatio nal task: adding two numbers. This
guide walks yo u thro ugh each step, fro m declaring variables to getting the
o utput, making it a perfect starting...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

What is the Tower of Hanoi problem?

How do you solve the Tower of Hanoi problem in C?

What is the base case in the recursive solution f or the Tower of


Hanoi?

How do you calculate the minimum number of moves required to solve


the Tower of Hanoi f or 'n' disks?

Can the Tower of Hanoi be solved iteratively?

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy