Search C Program | nimishsoft@gmail.com

Towers of Hanoi by recursion

/* Towers of Hanoi by recursion */

#include<stdio.h>
void toh(int,char,char,char);

int main()
{
 int n=3;
 toh(n,'A','B','C');
 return 0;
}

void toh(int n,char a,char b,char c)
{
 if(n==1)
  printf("\nMoved from %c to %c",a,c);
 else
 {
  toh(n-1,a,c,b);
  toh(1,a,' ',c);
  toh(n-1,b,a,c);
 }
}
 

2 comments: