{============================================================== hanoi.p Started by Jeff Ondich some time in 1993 Last modified 10/23/96 This program prints out the moves involved in solving the Towers of Hanoi problem. ==============================================================} program hanoi(input,output); var nRings : integer; {============================================================== MoveRings solves the Towers of Hanoi problem for moving N rings from peg "start" to peg "destination," using peg "spare" for intermediate steps. The solution consists of a printed list of instructions. ==============================================================} procedure MoveRings( N, start, desination, spare : integer ); begin if N >= 1 then begin MoveRings( N - 1, start, spare, desination ); write( 'Move the top ring from peg ' ); writeln( start:1, ' to peg ', desination:1 ); MoveRings( N - 1, spare, desination, start ) end end; {============================================================== Main program ==============================================================} begin write( 'Enter the number of rings: ' ); readln( nRings ); writeln; writeln( 'To move ', nRings:1, ' rings from peg 1 to peg 3, ' ); writeln; MoveRings( nRings, 1, 3, 2 ) end.