News

Sunday, April 24, 2022

Tower of Hanoi in C++ Recursive Method



Introduction:

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Working Principle:

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Example:

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Code:

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------





// C++ recursive function to
// solve tower of hanoi puzzle
#include <bits/stdc++.h>

using namespace std;

void towerOfHanoi(int n, char source, char temporary, char destination)
{
	if (n == 0)
	{
		cout << "Zero Disks. So no need to move any disk. "<< endl;
		
	}
	else if (n == 1)
	{
		cout << "disk " << n << " moved from rod " << source << " to rod " << destination << endl;
		
	}
	else
	{
		towerOfHanoi( n-1, source, destination, temporary );
	    cout << "disk " << n << " moved from rod " << source << " to rod " << destination << endl;
	    towerOfHanoi( n-1, temporary, source, destination );
	}
	
}

// Driver code
int main()
{
	int n; 
	cout << "Enter number of disks = "; // Number of disks
	cin >> n;
	cout << endl << "Total moves will be = " << (pow(2,n)-1) ; 
	cout << endl << endl;
	towerOfHanoi(n, 'S', 'T', 'D'); // A, B and C are names of rods
	return 0;
}





Output:

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------




No comments:

Post a Comment