Monday 31 October 2016

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.
Prolog Program to find maximum of two numbers.
Video on Prolog Program to find the maximum of two numbers.

2 comments:

  1. thanks sir....if i wana return maximum of three numbers?

    ReplyDelete
    Replies
    1. 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).

      Delete