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