### Towers of Hanoi Using Iteration

I am trying to write a towers of hanoi c++ program using iteration. i have written the same program using recursion but i am having trouble using iteration. so far this is what i have:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````#include using namespace std; int exp(int n){ int two=1; for (int i=0; i> disk; cout << endl; if ( disk <= 0) cout << "You entered an invalid number." << endl; else if ( disk % 2 == 0 ){ for ( int j=1; j<=exp(disk); j++){ cout << "Move disk " << "1" << " from tower A to tower B." << endl; cout << "Move disk " << "2" << " from tower A to tower C." << endl; cout << "Move disk " << "3" << " from tower B to tower C." << endl; } } else if( disk % 2 == 1){ for ( int j=1; j<=exp(disk); j++){ cout << "Move disk " << "1" << " from tower A to tower C." << endl; cout << "Move disk " << "2" << " from tower A to tower B." << endl; cout << "Move disk " << "3" << " from tower C to tower B." << endl; } } return 0; } ``````

the program needs to allow the user to enter the number of disk they want to use then display instructions on how to complete the game.

i was thinking of using a nested loop but that was not working.
Topic archived. No new replies allowed.