Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Algorithm for Intelligent Systems and Robotics Lab

Experiment -2
Tower of Hanoi Problem

Name – Abhit Yadav


Sap Id – 500096635
Roll no. – R2142211143
Batch – B5
Write a code to find the solution of tower of Hanoi problem
with three pegs source, auxiliary and destination where n (no. of
disks) is taken as an input from user in python.
Input –

def tower_of_hanoi(n, source, auxillary, destination):


if n == 1:
print(f"Move disk 1 from {source} to {destination}")
return
tower_of_hanoi(n - 1, source, destination, auxillary)
print(f"Move disk {n} from {source} to {destination}")
tower_of_hanoi(n - 1, auxillary, source, destination)

n = int(input("Enter the no. of disks (N): "))


tower_of_hanoi(n, 'source', 'auxillary', 'destination')

Output –

You might also like