C Program for Tower of Hanoi Problem using Recursion


Description:
The tower of Hanoi is a traditional problem, which consists of moving n disks from source to destination using a temporary tower; main constraint is that the disks should be arranged such that larger disks are always below the smaller ones.

For More on Tower of Hanoi, Visit:
http://en.wikipedia.org/wiki/Tower_of_Hanoi


Source Code:
/* 
Tower of Hanoi using Recursion in C 
Author: sourcecodesonline.blogspot.com
*/  
#include<stdio.h>  
void Hanoi(int,char,char,char);  
int main(void)  
{              int no_disks;  
            char source='S',destination='D',temp='T';  
            printf("\n\t\t\t\tTower of Hanoi");  
            printf("\n\tEnter the no. of disks:");  
            scanf("%d",&no_disks);  
            printf("\nSource=S\tDestination=D\tTemporary=T");  
            Hanoi(no_disks,source,destination,temp);  
 return 0;  
}  
void Hanoi(int no_disks,char source,char destination,char temp)  
{  
            if(no_disks>0)  
             {  
                        Hanoi(no_disks-1,source,temp,destination);  
                        printf("\nMove disk %d from %c to %c",no_disks,source,destination);  
                        Hanoi(no_disks-1,temp,destination,source);  
             }  
Previous
Next Post »

Still not found what you are looking for? Try again here.