Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube

Tuesday 1 October 2013

Tower of Hanoi program in C language

C program for Tower of Hanoi using Recursion. Following is a C program of Tower of Hanoi.

 #include <stdio.h>  
 void towers(int, char, char, char);  
 int main()  
 {  
   int num;  
   printf("Enter the number of disks : ");  
   scanf("%d", &num);  
   printf("The sequence of moves involved in the Tower of Hanoi are :\n");  
   towers(num, 'A', 'C', 'B');  
   return 0;  
 }  
 void towers(int num, char frompeg, char topeg, char auxpeg)  
 {  
   if (num == 1)  
   {  
     printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);  
     return;  
     }  
     towers(num - 1, frompeg, auxpeg, topeg);  
     printf("\nMove disk %d from peg %c to peg %c", num, frompeg, topeg);  
     towers(num - 1, auxpeg, topeg, frompeg);  
 }  

0 comments :

Post a Comment