Category Archives: RAI

Implementing Calculator in Prolog

Prolog is a logic programming language widely used in artificial intelligence and symbolic computing. Unlike imperative languages, Prolog is declarative, meaning it defines relationships rather than a step-by-step procedure. In this blog, we will implement a simple calculator in Prolog that can perform basic arithmetic operations: addition, subtraction, multiplication, and division.

Complete Code

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
Continue reading Implementing Calculator in Prolog