C program for Tower of Hanoi.
Aim– We have to move all disc from source pillar to destination pillar.
Rules– Discs can be moved one at a time only.
We have to put smaller disk over the bigger disc.
Trick– 1. First, move the disk from source to auxiliary.
2. Then, move the disc from auxiliary to the destination.
Watch video to clear your doubts- Click Here
Here is your code-
#include <stdio.h> // header files #include <stdlib.h> int disc; char s='A',a='B',d='C'; int hanoi(int,char,char,char); // prototype int main() // main function { printf("\n This is a simple program for the tower of Hanoi"); printf("\n Enter the no. of discs you wanna move from ---->Source:A to Destination:C\n"); scanf("%d",&disc); // taking input as no. of disc hanoi(disc,s,a,d); // calling of function return 0; } int hanoi(int disc,char s,char a,char d) // function definition { if(disc==1) { printf("\n move disc %d from %c to %c", disc,s,d); return; } hanoi(disc-1,s,d,a); // function called again called recursion printf("\n Move Disc %d From %c to %c",disc,s,d); hanoi(disc-1,a,s,d); // function called again called recursion return 0; } // end of function definition
Output:



The above code can’t be directly copy-pasted. You must have to type with your own hand. This is made for your better future.
Are you made for copy paste?
Absolutely “NO”
Then, try hard to understand this code and type with your own.