Answers for "tower of hanoi in c"

C++
1

tower of hanoi c++

//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!
Posted by: Guest on November-05-2020
0

Tower of Hanoi in c

#include <stdio.h>
void TOH(int n, int a, int b, int c)
{
 if(n>0)
 {
   TOH(n-1, a, c, b); //a to b using c
   printf("%d, %d n", a,c);
   TOH(n-1, b, a, c); //b to c using a
 }
}

int main(void) {
 TOH(3,1,2,3);
 return 0;
}
Posted by: Guest on November-25-2021

Browse Popular Code Answers by Language