2 Aug 2019

  • August 02, 2019
  • Amitraj



Towers of Hanoi problem using recursion:-

This C Program uses recursive function & solves the tower of hanoi. The tower of hanoi is a mathematical puzzle. It consists of threerods, and a number of disks of different sizes which can slideonto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top. We have to obtain the same stack on the third rod.

Here is the source code of the C program for solving towers of hanoi.

/*  C Program to solve towers of hanoi problem using recursion*/   By Amit raj purohit

http://codevidyalay.blogspot.com

#include<stdio.h>
void hanoi (int,char,char,char);
int main()
{
int n;

char s='L',i='C',d='R';
printf("Enter how many Disks:");
scanf("%d",&n);

hanoi(n,s,i,d);
return 0;
}
void hanoi(int n,char s,char i,char d)
{
if(n!=0)
{

hanoi(n-1,s,d,i);
printf("\nMove %d from %c to %c",n,s,d);
hanoi(n-1,i,s,d);
}
}


INPUT/OUTPUT:
Enter how many Disks:2
Move 1 from L to C
Move 2 from L to R
Move 1 from C to R







Translate

Popular Posts