Implementing Calculator in Prolog

PREDICATES
ADD(integer,integer,integer)
SUB(integer,integer,integer)
MUL(integer,integer,integer)
DIV(integer,integer,integer)

CLAUSES
ADD(A,B,SUM):-
SUM=A+B.
SUB(A,B,DIF):-
DIF=A-B.
MUL(A,B,MUL):-
MUL=A*B.
DIV(A,B,DIV):-
DIV=A/B.

//output
Goal: ADD(5,4,SUM)
SUM=9
1 solution
Goal: SUB(9,4,SUB)
SUB=5
1 solution
Goal: MUL(4,5,MUL)
MUL=20
1 solution
Goal: DIV(10,2,DIV)
DIV=5
1 solution

Leave a Reply

Your email address will not be published. Required fields are marked *