Write a Prolog program to find the maximum of two numbers.
% Write a prolog program to find the maximum of two numbers.max(X,Y):-
(
X=Y->write('both are equal')
;
X>Y->
(
Z is X,
write(Z)
)
;
(
Z is Y,
write(Z)
)
).
% Output
Prolog Program to find maximum of two numbers.
Video on Prolog Program to find the maximum of two numbers.
You can use the below mentioned code to find the max from a list (where you can just enter three numbers to solve your case):
domains list = integer* Max = integer predicates maximum_no(list,integer) clauses maximum_no([],Max):- write("Maximum No in List is:: ",Max). maximum_no([H|T],Max):- H>Max, N = H, maximum_no(T,N). maximum_no(L,Max):- maximum_no(L,Max).
thanks sir....if i wana return maximum of three numbers?
ReplyDeleteYou can use the below mentioned code to find the max from a list (where you can just enter three numbers to solve your case):
Deletedomains
list = integer*
Max = integer
predicates
maximum_no(list,integer)
clauses
maximum_no([],Max):-
write("Maximum No in List is:: ",Max).
maximum_no([H|T],Max):-
H>Max,
N = H,
maximum_no(T,N).
maximum_no(L,Max):-
maximum_no(L,Max).