Tower of Hanoi Problem
📖 Scenario: Imagine you have three rods and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks neatly stacked in ascending order of size on one rod, the smallest at the top, making a conical shape.The objective is to move the entire stack to another rod, obeying the following simple rules:Only one disk can be moved at a time.Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or on an empty rod.No disk may be placed on top of a smaller disk.
🎯 Goal: You will write a C program that uses a recursive function to solve the Tower of Hanoi problem for 3 disks. The program will print each move showing which disk moves from which rod to which rod.
📋 What You'll Learn
Create an integer variable
n to represent the number of disks (3).Create a recursive function
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) that prints the moves.Call the
towerOfHanoi function with the correct arguments to solve the puzzle.Print each move in the format:
Move disk X from rod Y to rod Z.💡 Why This Matters
🌍 Real World
The Tower of Hanoi problem helps understand recursion, a key concept in programming and problem solving.
💼 Career
Recursion is used in many software engineering tasks like parsing, searching, and algorithm design.
Progress0 / 4 steps