Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Thursday, 30 November 2017

C program to implement tower of Hanoi by recursion

  1. #include <stdio.h> #include <conio.h> void hanoi(char,char,char,int); void main() { int num; clrscr(); printf("\nENTER NUMBER OF DISKS: "); scanf("%d",&num); printf("\nTOWER OF HANOI FOR %d NUMBER OF DISKS:\n", num); hanoi('A','B','C',num); getch(); } void hanoi(char from,char to,char other,int n) { if(n<=0) printf("\nILLEGAL NUMBER OF DISKS"); if(n==1) printf("\nMOVE DISK FROM %c TO %c",from,other); if(n>1) { hanoi(from,other,to,n-1); hanoi(from,to,other,1); hanoi(to,from,other,n-1); }

No comments:

Post a Comment