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






Related Posts:

  • Towers of Hanoi problem using recursion .. 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… Read More
  • Matrix multiplication in C Matrix multiplication in C C program to multiply two matrices (two-dimensional arrays) which will be entered by a user. The user will enter the order of a matrix and then its elements and similarly inputs the second m… Read More
  • Armstrong Number in C Armstrong Number in C Before going to write the c program to check whether the number is Armstrong or not, let's understand what is Armstrong number. Armstrong number is a number that is equal to the sum of cub… Read More
  • Perfect Number in C Perfect Number in C Here you will get program for perfect number in C. Perfect number is a positive number which is equal to the sum of all its divisors excluding itself. For example: 28 is a perfect number as… Read More
  • Fibonacci Series in C Fibonacci Series in C Fibonacci Series in C:  In case of fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fib… Read More

Translate

Popular Posts