C Program for Tower of Hanoi

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

Tower of Hanoi is a purely mathematics-based puzzle game that consists of three rods with a number of disks of varying diameters arranged in ascending order of diameter from the top, thus forming a conical shape consisting of disks. The aim of the game is to move the stack of disks from the first rod to the last rod using an auxiliary rod by following certain rules. The rules of the game are as follows:

  1. One disk can be moved only once at a time.
  2. Upper disk can be moved from one of the stacks and placed on top of another empty rod.
  3. Disk of the largest diameter can't be placed on a disk of the smaller diameter.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Introduction to Tower of Hanoi in C

To solve the Tower of Hanoi Program in C programming language, the most basic implementation is by writing a program using

  1. Recursive Method
  2. Iterative method

tower-of-hanoi-example

C Program to Solve Tower of Hanoi Using Recursive Method

To solve the problem of the Tower of Hanoi in C, using the recursive method, We will consider the following:

Three rods A, B and C: where A is the rod from which the disks will be shifted. A acts as a source. B is the auxiliary rod using which the disks are moved to C. C is the destination rod.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Explanation

  • Tower of Hanoi when only one disk and towers A, B, and C are given:

tower-of-hanoi-with-one-disk

  • Tower of Hanoi when 2 disks are given with towers A, B, and C:

tower-of-hanoi-with-two-disk

  • Tower of Hanoi when 3 disks are provided with rods A,B, and C:

tower-of-hanoi-with-three-disk

Similarly.

  • Tower of Hanoi when n disks are given with rods A, B, and C

Note: For 'n' disks number of moves= (2n1)(2^{n-1}).

Let's look into the implementation of the following program using the C programming language.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Example

Output:

C Program to Solve Tower of Hanoi Using Iterative Method

Here also we'll consider 3 rods A, B and C as the source rod, auxiliary rod and destination rod.

tower-of-hanoi-step-by-step

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Explanation

When i=1, we will shift the disk from source to destination.

When i=2, the auxiliary rod is empty so we move the disk from the source rod to the auxiliary rod.

When i=3, we need to shift the remaining disk to the destination rod and for it, we will shift the disk from destination to auxiliary.

In a similar way, we observe a pattern here, ie., after 3 subsequent moves, the destination rod becomes acceptable to get a disk(That's the reason we use i%3).

Example

Output:

Complexity of Tower of Hanoi in C

Time complexity of Tower of Hanoi using the

iterative method: O(n)O(n)
Space complexity: O(n)O(n)

Time complexity of Tower of Hanoi using

Recursive method: O(2n)O(2^n)
Space complexity: O(n)O(n)

Ready to conquer complex algorithms? Our Dynamic Programming Certification course is your ticket to success. Enroll today and elevate your coding proficiency!

Conclusion

  • We learned two approaches to solving the Tower of Hanoi program in C programming language: The iterative method and the Recursive method.
  • Three rods are considered as a source, destination, and auxiliary rods for transferring the disks.
  • The number of moves required for 'n' disks is 2n12^{n-1}.
  • Time complexity of the Tower of Hanoi program in C using the iterative method is O(n) and using the recursive method is O(2n)O(2^n).
  • Space complexity for both auxiliary and iterative approaches for solving the Tower of Hanoi program in c is O(n).