0% found this document useful (0 votes)
16 views2 pages

Tower of Hanoi

Explanation of the c solution to the tower of hanoi
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)
16 views2 pages

Tower of Hanoi

Explanation of the c solution to the tower of hanoi
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/ 2

Let's discuss the Tower of Hanoi problem.

Source provides a C program that uses a recursive function to solve the Tower of Hanoi
puzzle.

Understanding the Tower of Hanoi

The Tower of Hanoi is a classic puzzle where you have three pegs (A, B, and C) and a
number of disks of different sizes that can slide onto any peg. The puzzle starts with the disks
stacked in decreasing size on peg A, the smallest disk being at the top, thus making a conical
shape.

The goal of the puzzle is to move the entire stack of disks from peg A to peg C, following
these rules:

• Only one disk can be moved at a time.


• A larger disk cannot be placed on top of a smaller disk.
• Disks can only be moved between the three pegs.

Recursive Solution

The provided C program in source uses recursion to solve this problem. Recursion is a
programming technique where a function calls itself to break down a problem into smaller,
self-similar subproblems. In the context of the Tower of Hanoi:

1. Base Case: If there's only one disk (num == 1), the solution is straightforward: move
the disk directly from the source peg (frompeg) to the destination peg (topeg).
2. Recursive Steps: If there are multiple disks, the solution is broken down into these
steps:
o Move the top num - 1 disks from the source peg (frompeg) to the auxiliary
peg (auxpeg), using the destination peg (topeg) as the temporary peg.
o Move the largest disk (disk num) from the source peg (frompeg) to the
destination peg (topeg).
o Move the num - 1 disks from the auxiliary peg (auxpeg) to the destination
peg (topeg), using the source peg (frompeg) as the temporary peg.

Program Explanation

Let's break down the code from Source:

#include <stdio.h>
void towers(int, char, char, char);
int main()
{
int num;
printf("Put the value of disks: ");
scanf("%d", &num);
printf("In the Tower of Hanoi, the following moves are made
in succession:\n");
towers(num, ‘A’, ‘C’, ‘B’);
return 0;
}
void towers(int num, char frompeg, char topeg, char auxpeg)
{
// Base Condition if there are no discs
if (num == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", frompeg,
topeg);
return;
}
// twice calling a function recursively

towers(num - 1, frompeg, auxpeg, topeg);


printf("\n Move disk %d from peg %c to peg %c", num, frompeg,
topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}

• towers() function: This function implements the recursive solution. It takes four
arguments:
o num: The number of disks to move.
o frompeg: The peg where the disks start.
o topeg: The peg where the disks need to be moved.
o auxpeg: The auxiliary peg used for temporary storage.
• main() function:
o Gets the number of disks (num) from the user.
o Calls the towers() function to start the puzzle-solving process. In this initial
call, 'A' is the source peg, 'C' is the destination peg, and 'B' is the auxiliary peg.

Output Explanation

Source illustrates the output of the program when the user inputs '2' for the number of disks:

Put the value of disks: 2


In the Tower of Hanoi, the following moves are made in succession:

Move disk 1 from peg A to peg B


Move disk 2 from peg A to peg C
Move disk 1 from peg B to peg C

The output shows the step-by-step moves required to solve the puzzle for two disks. Each line
indicates which disk is moved from which peg to which peg.

Key Concepts

• Recursion: The program effectively uses recursion to break down the complex task
of moving multiple disks into a series of simpler moves.
• Base Case: The base case (num == 1) is crucial in a recursive function to stop the
recursion and provide a direct solution for the simplest scenario.
• Tower of Hanoi Logic: The recursive steps in the towers() function implement the
core logic of solving the Tower of Hanoi puzzle.
• Output Clarity: The output is presented in a clear and understandable way, making it
easy to follow the solution.

You might also like