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

2 thoughts on “Implementing Calculator in Prolog”

    1. try this
      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.

      GOAL
      ADD(5,4,SUM),
      SUB(5,4,DIF),
      MUL(5,4,MUL),
      DIV(5,4,DIV).

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.