Question

write aprogram to slove tower of hanoi problem recursively in c

write aprogram to slove tower of hanoi problem recursively in c

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

code:

#include <stdio.h>

void tower_of_hanoi(int number, char frompeg, char topeg, char auxpeg);

int main()
{
int number;

printf("Please enter the number of disks : ");
// asking the user to enter the number of disks
scanf("%d", &number);
printf("Steps involved in the tower of hanoi are :\n");
// calling the tower_of_hanoi function
tower_of_hanoi(number, 'A', 'C', 'B');
return 0;
}
void tower_of_hanoi(int number, char frompeg, char topeg, char auxpeg)
{
if (number == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}
tower_of_hanoi(number - 1, frompeg, auxpeg, topeg);
printf("\n Move disk %d from peg %c to peg %c", number, frompeg, topeg);
tower_of_hanoi(number - 1, auxpeg, topeg, frompeg);
}

code screenshot:

include <stdio.h> 2 void tower of hanoi (int number, char frompeg, char topeg, char auxpeg); 4 int main () 5 6 E int number;

output:

DADesktop cg\tower of_hanoi.exe Please enter the number of disks 3 Steps involved in the tower of hanoi are Move disk 1 from

Add a comment
Know the answer?
Add Answer to:
write aprogram to slove tower of hanoi problem recursively in c
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT