macintosh_cookbook/examples/towerofhanoi.pas

24 lines
468 B
ObjectPascal
Raw Normal View History

2020-05-07 09:47:11 +00:00
program TowersOfHanoi (input, output);
var
disks: Integer;
procedure Hanoi (source, temp, destination: char; n: integer);
begin
if n > 0 then
begin
Hanoi(source, destination, temp, n - 1);
Writeln('Move disk ', n : 1, ' from peg ', source, ' to peg ', destination);
Hanoi(temp, source, destination, n - 1);
end;
end;
begin
Write('Enter the number of disks: ');
Readln(disks);
Writeln('Solution:');
Hanoi('A', 'B', 'C', disks);
end.