You are on page 1of 16

Program 1

Write a prolog program to study fact, domain, predicate and


clause section.

Fact:- A fact is predicate expression that make a declarative statement


about the problem domain.
Predicate:- ‘Predicate’ is the name given to the word occurring before
the bracket in a fact or rule.
Clause:- Predicate definition consist of clauses. An individual
definition(whether it be a fact or rule).
A clause consists of a head and sometimes a body.
Facts don’t have a head because they are always true.
Domain:- Domains enable you to give distinctive name to different
kinds of data that would otherwise look alike. In a Prolog program,
object in a relation(the arguments to be predicate) belong to
domains; these can be pre-defined domains, or special domains that
you specify.

Source Code-
%DOMAINS
product,sum = integer.
%FACTS AND PREDICATES

add_num(sum,sum,sum).
multiply_num(product,product,prod).
%CLAUSES

add_num(X,Y,Sum):-Sum is X+Y.
multiply_num(X,Y,Product):-Product
is X*Y.

BCA 5TH 1 AMIT KUMAR JHA |


OUTPUT-

BCA 5TH 2 AMIT KUMAR JHA |


Program 3
Write a prolog program to check greater number.
max(X,Y):- (
X=Y ->
write('both are equal')
;
X>Y -> (
Z is X, write(Z)
)
;
(
Z is Y, write(Z)
)
).

OUTPUT-

BCA 5TH 3 AMIT KUMAR JHA |


Program 4
Write a prolog program of quadratic equation.
solve(A*_^2+B*_+C=0,L) :- D is B^2-4*A*C,
(D < 0 -> L = [];
D =:= 0 -> X is (-B)/(2*A), L = [X];
S is sqrt(D), X1 is (-B-S)/(2*A),
X2 is (-B+S)/(2*A), L=[X1,X2]).

OUTPUT-

BCA 5TH 4 AMIT KUMAR JHA |


Program 5
Write a Prolog program to implement water jug problem.

water_jug(X,Y):-X>4,Y<3,write('4L Jug Overflowed!'),nl. water_jug(X,Y):-


X<4,Y>3,write('3L Jug Overflowed!'),nl. water_jug(X,Y):-X>4,Y>3,write('Both
Jugs Overflowed!'),nl. water_jug(X,Y):-(X=:=0,Y=:=0,nl,write('4L:0 &
3L:3(Action: Fill 3L Jug)'),YY is
3,water_jug(X,YY));
(X=:=0,Y=:=0,nl,write('4L:4 & 3L:0(Action: Fill 4L Jug)'),XX is
4,water_jug(XX,Y));
(X=:=2,Y=:=0,nl,write('4L:2 & 3L:0(Action: Goal State Reached...)'));
(X=:=4,Y=:=0,nl,write('4L:1 & 3L:3(Action: Pour water from 4L to 3L
Jug)'),XX is X3,YY is 3,water_jug(XX,YY));
(X=:=0,Y=:=3,nl,write('4L:3 & 3L:0(Action: Pour water from 3L to 4L
Jug)'),XX is 3,YY is 0,water_jug(XX,YY));
(X=:=1,Y=:=3,nl,write('4L:1 & 3L:0(Action: Empty 3L Jug)'),YY is
0,water_jug(X,YY));
(X=:=3,Y=:=0,nl,write('4L:3 & 3L:3(Action: Fill 3L Jug)'),YY is
3,water_jug(X,YY);
(X=:=3,Y=:=3,nl,write('4L:4 & 3L:2(Action: Pour water from 3L Jug to 4L Jug
until
4L Jug is full)'),XX is X+1,YY is Y-1,water_jug(XX,YY));
(X=:=1,Y=:=0,nl,write('4L:0 & 3L:1(Action: Pour water from 4L to 3L
jug)'),XX is Y,YY is X,water_jug(XX,YY));
(X=:=0,Y=:=1,nl,write('4L:4 & 3L:1(Action: Fill 4L Jug)'),XX is
4,water_jug(XX,Y));
(X=:=4,Y=:=1,nl,write('4L:2 & 3L:3(Action: Pour water from 4L Jug to 3L Jug
until
3L Jug is full)'),XX is X-2,YY is Y+2,water_jug(XX,YY));
(X=:=2,Y=:=3,nl,write('4L:2 & 3L:0(Action: Empty 3L Jug)'),YY is
0,water_jug(X,YY));
(X=:=4,Y=:=2,nl,write('4L:0 & 3L:2(Action: Empty 4L Jug)'),XX is
0,water_jug(XX,Y));
(X=:=0,Y=:=2,nl,write('4L:2 & 3L:0(Action: Pour water from 3L Jug to 4L
Jug)'),XX is Y,YY is X, water_jug(XX,YY))).

BCA 5TH 5 AMIT KUMAR JHA |


OUTPUT-

BCA 5TH 6 AMIT KUMAR JHA |


Program 7
Write a Prolog program to Enter The Login Details.

login:-(getinput(_,_)
-> write("You are logged in successful"),nl;
write(" invalid user")).

user(sneha,1234).
getinput(A,B):- write("\n
Welcome,please login"),nl,
write("Enter your name:"),
read(A),nl, write("Enter your
password"), read(B),nl,
user(A,B).

OUTPUT-

BCA 5TH 7 AMIT KUMAR JHA |


Program 8
Write a prolog program to convert the given string in upper case
letters into lower case letters and vice versa.

write("Enter string to convert\


n"), read(String), write("String
in Uppercase is "),nl,
string_upper(String,A),write(A),
nl, write("String in Lowercase
is "),nl,
string_lower(String,B),write(B).

OUTPUT-

BCA 5TH 8 AMIT KUMAR JHA |


Program 7
write a prolog program for implement string
atom_string("x",'x').
atom_string('x',"x").
atom_string(3.1415,3.1415).
atom_string('3r2',3r2).
atom_string(3r2,'3r2').
atom_string(6r4,3r2).
atomics_to_string([gnu, "gnat", 1], ', ', A).
A = "gnu, gnat, 1"

OUTPUT-

BCA 5TH 9 AMIT KUMAR JHA |


Program 8
write a prolog program for compound
parent(pam,bob).
parent(tom,bob).
parent(tom,liz).

parent(bob,ann).
parent(bob,pat).
parent(bob,jim).
male(jim). male(tom). male(bob).

female(pam).
female(liz).
female(ann).
female(pat).

different(ann,pat).

offspring(Y,X):-parent(X,Y). mother(X,Y):-
parent(X,Y),female(X). grandparent(X,Z):-
parent(X,Y),parent(Y,Z). sister(X,Y):-
parent(Z,X),parent(Z,Y),female(X),different(X,Y).
predecessor(X,Z):-parent(X,Z). predecessor(X,Z):-
parent(X,Y),predecessor(Y,Z).

BCA 5TH 1 AMIT KUMAR JHA |


OUTPUT-

BCA 5TH 1 AMIT KUMAR JHA |


Program
Write a prolog program to show use of input,output,Cut And Fail
predicate.

cube:-
write("Enter a number:"), read(Number), process(Number).
process(stop):-!. process(Number):-
C is Number* Number*Number, write("Cube
of"),write(Number),write(":"),write(C),nl,cube.

OUTPUT-

BCA 5TH 1 AMIT KUMAR JHA |


Program 13
Write a prolog program to show use of list.
member(X,[X|Tail]).
/*concatenate*/ member(X,[Head|
Tail]):- member(X,Tail).
conc([],L,L).
conc([X|M],N,[X|
included_list([X],_).
Q]):-
included_list([Head|Tail],List):-
conc(M,N,Q). member(Head,List), included_list(Tail,List).

OUTPUT-

BCA 5TH 1 AMIT KUMAR JHA |


Program 14
Write a prolog program to study usage of recursion in prolog.
factorial(0,1).
factorial(N,F):-
N>0, N1 is N-1,
factorial(N1,F1), F
is N * F1.

OUTPUT-

BCA 5TH 1 AMIT KUMAR JHA |


Program 15
Write a prolog program to accept
• Name of the student
• Roll no.
• Subject name
• Maximum marks
• Marks obtained
Compute the percentage of the student and display his result with other
information.
total_marks(R1,R2,R3,R4,R5,Total):-
Total is(R1 + R2 + R3 + R4 + R5).
percentage(Marks, Percent):-
Percent is ((Marks / 500) * 100).
passOrFail(Marks):-
Marks < 200 -> write("So Sorry, You are fail");write("You are Pass").

student:-
write("Enter your name :"),nl,
read(Name), write("Enter
your roll no :"),nl, read(Roll),
write("Enter your Subjects"),nl,
write("Subject 1 : "),read(S1),
write("Subject 2 : "),read(S2),
write("Subject 3 : "),read(S3), write("Subject 4 :
"),read(S4), write("Subject 5 : "),read(S5),nl,
write("Enter your marks"),nl, write("Marks in
"),write(S1),write(" : "),read(R1), write("Marks in

BCA 5TH 1 AMIT KUMAR JHA |


"),write(S2),write(" : "),read(R2), write("Marks in
"),write(S3),write(" : "),read(R3), write("Marks in
"),write(S4),write(" : "),read(R4), write("Marks in
"),write(S5),write(" : "),read(R5),nl,nl,
write("Result"),nl, write("Name :
"),write(Name),nl, write("Roll no :
"),write(Roll),nl, write("Marks obtained :
"),total_marks(R1,R2,R3,R4,R5,Total),write(Total),write(" / 500"),nl,
write("Percentage is : "),percentage(Total,Percent),write(Percent),write("
% "),nl,
passOrFail(Total).

OUTPUT-

BCA 5TH 1 AMIT KUMAR JHA |

You might also like