A
B
C
program TowerOfHanoi;
procedure hanoi(n: integer; fromPeg, toPeg, auxPeg: char);
begin
if n = 1 then
writeln('Move disk 1 from ', fromPeg, ' to ', toPeg)
else
begin
hanoi(n - 1, fromPeg, auxPeg, toPeg);
writeln('Move disk ', n, ' from ', fromPeg, ' to ', toPeg);
hanoi(n - 1, auxPeg, toPeg, fromPeg);
end;
end;
begin
hanoi(3, 'A', 'C', 'B');
end.
#include <stdio.h>
void hanoi(int n, char from, char to, char aux) {
if (n == 1) {
printf("Move disk 1 from %c to %c\n", from, to);
return;
}
hanoi(n - 1, from, aux, to);
printf("Move disk %d from %c to %c\n", n, from, to);
hanoi(n - 1, aux, to, from);
}
void hanoi(int n, char from, char to, char aux) {
if (n == 1) {
cout << "Move disk 1 from " << from << " to " << to << endl;
return;
}
hanoi(n - 1, from, aux, to);
cout << "Move disk " << n << " from " << from << " to " << to << endl;
hanoi(n - 1, aux, to, from);
}
function hanoi(n: number, from: string, to: string, aux: string): void {
if (n === 1) {
console.log(\`Move disk 1 from \${from} to \${to}\`);
return;
}
hanoi(n - 1, from, aux, to);
console.log(\`Move disk \${n} from \${from} to \${to}\`);
hanoi(n - 1, aux, to, from);
}
function hanoi(n, from, to, aux) {
if (n === 1) {
console.log(`Move disk 1 from ${from} to ${to}`);
return;
}
hanoi(n - 1, from, aux, to);
console.log(`Move disk ${n} from ${from} to ${to}`);
hanoi(n - 1, aux, to, from);
}
def hanoi(n, from_peg, to_peg, aux_peg):
if n == 1:
print(f"Move disk 1 from {from_peg} to {to_peg}")
return
hanoi(n-1, from_peg, aux_peg, to_peg)
print(f"Move disk {n} from {from_peg} to {to_peg}")
hanoi(n-1, aux_peg, to_peg, from_peg)