Wisdom Materials
Home
About Us
Our Clients
Careers
Services
Education
Jobs
News
Business
Health
Astrology
Entertainment
RealEstate
Devotion
Contact Us
C Programs
/ Towers of Hanoi C Program
Program
Copy text
#include
void TowersHanoi(int n,char from,char to,char aux); void main() { int n; printf("Towers of Hanoi C Program \n"); printf("Enter number of disks:\n"); scanf("%d",&n); TowersHanoi(n,'A','C','B'); } void TowersHanoi(int n,char from,char to,char aux) { if(n==1) { printf("Move disk 1 from %c peg to %c peg\n",from,to); return; } TowersHanoi(n-1,from,aux,to); printf("Moves disk %d from %c peg to %c peg\n",n,from,to); TowersHanoi(n-1,aux,to,from); }
Input
Enter number of disks:3
Output
Move disk 1 from A peg to C peg
Moves disk 2 from A peg to B peg
Move disk 1 from C peg to B peg
Moves disk 3 from A peg to C peg
Move disk 1 from B peg to A peg
Moves disk 2 from B peg to C peg
Move disk 1 from A peg to C peg
Home
Back