Friday, 23 September 2016

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);
}
}

No comments:

Post a Comment

Featured post

Will AI Replace Programmers? The Truth Behind the Hype in 2025

Exploring the Future of Coding: Will AI Take Over Software Development or Enhance It?  Will AI Replace Programmers? The Truth Behind the Hyp...