Write a program in PROLOG to implement towerofhanoi (N) where N represents the number of discs.

8. Write a program in PROLOG to implement towerofhanoi (N) where N represents the number of discs.

% Write a program in PROLOG to implement towerofhanoi (N) where N 
  represents the number of discs.

%  below hanoi function is not predefined you can use any name as function

hanoi(N,F,T,A):-
( N=1 ->
( write('Move disk 1 from rod '),write(F),write(' to rod '),write(T),nl
)
;
Z is N-1,
hanoi(Z,F,A,T),
write('Move disk 1 from rod '),write(F),write(' to rod '),write(T),nl,
hanoi(Z,A,T,F)
).

% Output


No comments:

Post a Comment

Featured post

Amazon Interview Process

On July 5, 1994, Jeff Bezos started the world's most "customer-centric" firm out of his garage in Bellevue, Washington. The A...