Solved

Towers of Hanoi

Posted on 1997-05-20
1
490 Views
Last Modified: 2008-03-17
I need to get the code for the towers of hanoi in c++
I have been trying for days to figure it out.
"please help"
0
Comment
Question by:denver
1 Comment
 
LVL 5

Accepted Solution

by:
yonat earned 20 total points
Comment Utility
#include <iostream.h>

void Hanoi(char source, char temp, char destination, int n)
/* This functions takes a tower of n disks and moves from peg  */
/* 'source' to peg 'destination'.  Peg 'temp' may be used      */
/* temporarily.                                                */
{
        if (n>0)
        {
        Hanoi (source, destination, temp, n-1);
        cout << "Move disk "<<< n << " from peg " << source <<
                " to peg " << destination << ".\n";
        Hanoi ( temp, source, destination, n-1);
        }
}

main()
{       int n;
        cout << "Enter the number of disks: ";
        cin >> n;
        cout << '\n';
        Hanoi('A','B','C',n);
}


0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now