Tower of Hanoi

//code by Soumyadeep Ghosh
//insta : @soumyadepp
//linked in: https://www.linkedin.com/in/soumyadeep-ghosh-90a1951b6/
#include <bits/stdc++.h>

using namespace std;

void toh(int n,char a,char b, char c)
{
  if(n>0)
    {
        /*move n-1 disks from a to b using c*/
        toh(n-1,a,c,b);
        /*move a disc from a to c using b and display this step performed. Also note that a and c are different in the next called function*/
        cout<<"Move a disk from "<<a<<" to "<<c<<endl;
        toh(n-1,b,a,c);
    }
}
int main()
{
  int n;
  cin>>n;
  //names of the disks are a,b,c
  toh(n,'a','b','c');
  return 0;
}
//thank you!

0
0
Tengfred 95 points

                                    def towerOfHanoi(N , source, destination, auxiliary):
	if N==1:
		print(&quot;Move disk 1 from source&quot;,source,&quot;to destination&quot;,destination)
		return
	towerOfHanoi(N-1, source, auxiliary, destination)
	print(&quot;Move disk&quot;,N,&quot;from source&quot;,source,&quot;to destination&quot;,destination)
	towerOfHanoi(N-1, auxiliary, destination, source)
		
# Driver code
N = 3
towerOfHanoi(N,'A','B','C')
# A, C, B are the name of rods

0
0
4.5
8

                                    /// find total number of steps 
int towerOfHanoi(int n) {
  /// pow(2,n)-1
  if (n == 0) return 0;
  
  return towerOfHanoi(n - 1) + 1 + towerOfHanoi(n - 1);
}

4.5 (8 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
explain tower of hanoi tower of hanoi definition tower of hanoi implementation tower of hanoi example tower of hanoi location god of tower 3. Solve the towers of Hanoi problem using recursive function in python language (n=3 discs) Implement towers of Hanoi and calculate the number of recursive calls for n=3. TOWER OF HANOI RECURSIVE CODE There are four poles where ring can be placed. Poles-A and Pole-B contain two big andand two small rings respectively . The rule is: a big ring can not be placed on top of small ring. tower of hanoi using recursion tower of hanoi with restrictions tower of hanoi using stack tower of hanoi time complexity sequence of operation tower of hanoi tower of hanoi solution towers of hanoi in c tower of hanoi instructions program java using recursion tower of hanoi instructions program java tower of hanoi instuctions program java dry run of tower of hanoi tower of hanoi problem recursion tower of anoi simple recursion hanoi simple recursion Solve the problem of Tower of Hanoi for 4 disks using recursion tree and also write the steps to solve this problem. tower of hanoi recursive solution towers of hanoi solving algorithm towers of hanoi recursion towers of hanoi recursive solution tower f hanoi recursion tower of hanoi puzzle tower of hanoi program in cpp how to program tower of hanoi java tutorial towers of hanoi algorithm tower of hanoi JAVA hanoi tower algorithm tower of hanoi java code tower of hanoi recursion in c hanoi tower recursion towers of hanoi code tower of hanoi 5 disks solution java code for tower of hanoi taking disks, towers, starting and destination as inputs write a recursive function for tower of hanoi problem tower hennai is recursion or stack? tower of hanoi in data structure program hanoi recurcive c++ (2 * (n-1)) tower of hanoi algorithm in for loop in php tower of annoy algorithm in for loop in php tower of annoy where recursion occurs in tower of hanoi computer science hanoi tree Write a C program for Towers of Hanoi using Recursion. tower of hanoi logic tower of hanoi code Write pseudocode of Tower of Hanoi as application of stack tower od hanoi puzzle Derive the recurrent formula for tower of Hanoi and convert the recurrent formula to sum the number of movements required to move 5 discs in recursion tower of hanoi program the towers of hanoi recursive algorithm for hanoi tower Hanoi puzzle. f Hanoi puzzle. tower of hanoi dynamic programming tower hanoi dp algorithm tower of hanoi algorithm Implement the Towers of Hanoi Problem using graphical view 1. Implement the Towers of Hanoi Problem using graphical view tower of hanoi logic after 10 moves void tower of hnaoi towers of hanoi c++ 4 disks towers of hanoi c++ 4 tower of hanoi recursion towers of hanoi tower of hanoi
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source