added TowerOfHanoi.c

This commit is contained in:
shashikedissanayake 2017-09-21 17:47:09 +05:30
parent 071b275d42
commit dbe89ab930
1 changed files with 28 additions and 0 deletions

28
TowerOfHanoi.c Normal file
View File

@ -0,0 +1,28 @@
#include <stdio.h>
#include <stdlib.h>
void hanoi(int noOfDisks,char where,char to,char extra){
if(noOfDisks == 0 )
{
return;
}
else
{
hanoi(noOfDisks-1, where, extra , to);
printf("Move disk : %d from %c to %c\n",noOfDisks,where,to);
hanoi(noOfDisks-1,extra,to,where);
}
}
int main(void){
int noOfDisks;
//Asks the number of disks in the tower
printf("Number of disks: \n");
scanf("%d", &noOfDisks);
hanoi(noOfDisks,'A','B','C');
return 0;
}