0% found this document useful (0 votes)
33 views2 pages

13.b) Write Recursive Programs For Solving The Towers of Honoi Problem

This C program recursively solves the Towers of Hanoi problem by moving discs from one peg to another. It takes the number of discs as input, then calls a move function that recursively moves the top n-1 discs to a temporary peg, moves the nth disc to the destination peg, and then moves the n-1 discs from the temporary peg to the destination peg. The move function prints each step of the recursive solving process.

Uploaded by

navin_killer
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views2 pages

13.b) Write Recursive Programs For Solving The Towers of Honoi Problem

This C program recursively solves the Towers of Hanoi problem by moving discs from one peg to another. It takes the number of discs as input, then calls a move function that recursively moves the top n-1 discs to a temporary peg, moves the nth disc to the destination peg, and then moves the n-1 discs from the temporary peg to the destination peg. The move function prints each step of the recursive solving process.

Uploaded by

navin_killer
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

/* 13.

b)Write Recursive Programs for Solving the Towers of Honoi Problem */


#include<stdio.h>
void move(int,char,char,char);
void main()
{
int n;
clrscr();
printf("Enter number of discs\n");
scanf("%d",&n);
move(n,'s','t','d');
getch();
}
void move(int n,char src,char temp,char dest)
{
if(n==1)
{
printf(" \n1 %c--->%c\n",src,dest);
return;
}
move(n-1,src,dest,temp);
printf(" \n%d %c--->%c\n",n,src,dest);
move(n-1,temp,src,dest);
return;
}
/*
********************************OUTPUT******************************
Enter number of discs
3
1 s--->d
2 s--->t
1 d--->t
3 s--->d
1 t--->s
2 t--->d
1 s--->d
********************************************************************
*/

You might also like