C program to solve Tower of Hanoi problem for n disks using recursion.

Simple C program to solve Tower of Hanoi problem for n disks using recursion.

Code:

#include <stdio.h>

#include<conio.h>



void tower(int n, char from, char to, char through){

if (n==1){

printf("\n Move disk 1 from rod %c to rod %c", from, to);

return;

}

tower(n-1, from, through, to);

printf("\n Move disk %d from rod %c to rod %c", n, from, to);

tower(n-1, through, to, from);

}



main(){



int n;

clrscr();

printf("Enter the number of disk: ");

scanf("%d",&n);

tower(n, 'A', 'C', 'B');

getch();

}

Comments

Popular posts from this blog

C program to evaluate Prefix Expression using Stack data structure

Java Program to Implement sorting algorithm using TCP on Server application

C++ program to perform data transformation Min-max and Z score Normalization