Question

Make a Complete Code in C++ language. Create a Complete program of Tower of Hanoi. Write...

Make a Complete Code in C++ language. Create a Complete program of Tower of Hanoi. Write the code in easy way means coding should of beginner level. Also use more and more comments in the code for better understanding. The code should complete and should able to run. Also provide output of the program.

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

Dear student,

Here is the code in C++ using recursion for Tower Of Hanoi:

  1. #include<iostream>
  2. using namespace std;
  3. void TowerOfHanoi(int n,char a,char b,char c)// It is a recursive funtion
  4. {
  5. if(n>0)
  6. {
  7. TowerOfHanoi(n-1, a, c, b);//Recursion 1
  8. cout<<"Move a disk "<<n<<" from "<<" "<<a<<" to"<<" "<<c<<endl;
  9. TowerOfHanoi( n-1, b,a,c);//Recursion 2
  10. }// if ends
  11. }//function end
  12. int main()// execution begins here
  13. {
  14. int num;// num gives number of disks in tower
  15. cout<<"Enter the no. of disks"<<endl;
  16. cin>>num;
  17. // Now we call the recursive function
  18. TowerOfHanoi(num,'A','B','C');//A is the source rod , C is destination rod ,B is auxiliary rod
  19. cout<<endl;
  20. }//main function ends

Now, let us see the output of the above code if we take 4 disks:

Enter the no. of disks 4 Move a disk 1 from A to B Move a disk 2 from A to C Move a disk 1 from B to C Move a disk 3 from A to B Move a disk 1 from C to A Move a disk 2 from C to B Move a disk 1 from A to B 
Move a disk 4 from A to C Move a disk 1 from B to C Move a disk 2 from B to A Move a disk 1 from C to A Move a disk 3 from B to C Move a disk 1 from A to B Move a disk 2 from A to C
 Move a disk 1 from B to C

I hope the above code is simple and clear for you to understand.

Don't forget to give it a thumbs up.

Regards

Add a comment
Know the answer?
Add Answer to:
Make a Complete Code in C++ language. Create a Complete program of Tower of Hanoi. Write...
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
  • Make a Complete Code in C++ language. Create a Complete program of Tower of Hanoi using...

    Make a Complete Code in C++ language. Create a Complete program of Tower of Hanoi using stack implementation. The code should not copied means code should unique. Code should not copy paste from any where. Write the code in easy way means coding should of beginner level. Also use more and more comments in the code for better understanding. The code should complete and should able to run. Also provide output of the program. ( 1 ) Code should not...

  • Make a Code in C++ language. The Code should not copy paste from internet. The code...

    Make a Code in C++ language. The Code should not copy paste from internet. The code should complete and should run. Also provide output. Make the code in easy way and also provide comments. The code should complete and Don't forget to provide output. 1. Make a cpp class TTT. 2. In that class, you will have two functions. one main function and one function named fib(). 3. Get the value of n from user in main function. n specifies...

  • Recursion Write a program to solve the Towers of Hanoi problem for a tower of size...

    Recursion Write a program to solve the Towers of Hanoi problem for a tower of size n, using both recursion and iteration. Time each method separately. Be very carefull to time only the actual work and avoid superfluous module calls and initialization, etc. Compare and contrast your two versions of the problem. Are they what you expected? Your analysis must contain a table of the times obtained for each run. For a tower of a particular size, your output should...

  • Language C Code Write a program that takes two integer arrays (A and B) and sums...

    Language C Code Write a program that takes two integer arrays (A and B) and sums them together (element wise). A third array to accept the result should be passed in as the output argument. Assume the arrays are all the same size. The argument N is the size of the arrays. Your code should provide a function with the following signature: void array Addition (int A[], int B[], int N, int output[]) { } Your code must also provide...

  • *This is meant to be done in C++ language only* *Please consider the fact that I'm...

    *This is meant to be done in C++ language only* *Please consider the fact that I'm a beginner and want to be able to follow what you're doing so please write the code as simple and organized as possible. Thanks in advance. Create a program that uses an array(one dimensional) to store the results from a poll of 30 people. Each person was asked to estimate the amount of time, in minutes, that he or she spends on Facebook each...

  • Create an algorithm to count the number of 1’s in a 32-bit number. Implement the program in a high level language like...

    Create an algorithm to count the number of 1’s in a 32-bit number. Implement the program in a high level language like C or Java. It does not need to run for me, but the code should be included in a text document called FirstnameLastnameHLA3.txt along with your assignment submission. Implement the program in MIPSzy Assembly language. Use the high level code as comments to the right of the Assembly code as the textbook does. If you write that MIPSzy...

  • Create a complete pseudo-code program in C++ to do the following using the PseudoCode language developed...

    Create a complete pseudo-code program in C++ to do the following using the PseudoCode language developed in class using Absolute Addressing. There is 1 deliverable: 1. You should include the input cards shown below as a test. THE PROGRAM: First you will read in a value N which holds the number of values to be read in. (so if N is 20 then there will be 20 more cards to read in.) Read in N values into an array. Bubble-Sort...

  • Please write a recursive Java program to solve the Tower of Hanoi game for n disks...

    Please write a recursive Java program to solve the Tower of Hanoi game for n disks on pole A. Please read the textbook page 176 – 180 to fully understand this game or puzzle. The game consists of n disks and three poles: A (the source), B (the destination), and C (the spare). Initially, all the disks are on pole A. The game is to move all disks (one by one) from pole A to pole B using pole C...

  • Complete the following C code by filling all “???”sto write a C program to create a...

    Complete the following C code by filling all “???”sto write a C program to create a child process. Before the child process is created, an integer variable A is defined and assigned value 10. Then in the child process, A is reduced by 5. After the child process completes, the parent process increases A by 5. Then what will A’s value be just before the parent process completes? Why? Write the code and run it to verify your answer. #include...

  • In the language c using the isspace() function: Write a program to count the number of...

    In the language c using the isspace() function: Write a program to count the number of words, lines, and characters in its input. A word is any sequence of non-white-space characters. Have your program continue until end-of-file. Make sure that your program works for the case of several white space characters in a row. The character count should also include white space characters. Run your program using the following three sets of input:             1. You're traveling through ​               another...

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