Prolog PR Solution
Prolog PR Solution
Total Program 6:
Questions:
Q 1.For given facts write a prolog program to find out result of Particular Student
by inputting name.
Gaurav 9.2
Anooj 8.2
chinmay 7.3
Piyush 5.7
parth 8.9
ankit 6.3
tanuja 6.2
pooja 8.2
Asmita 7.2
Program 1: -
result(gaurav, 9.2).
result(anooj, 8.2).
result(chinmay, 7.3).
result(piyush, 5.7).
result(parth, 8.9).
result(ankit, 6.3).
result(tanuja, 6.2).
result(pooja, 8.2).
result(asmita, 7.2).
getresult:-
write("Whose result do you want to know"),nl,
read(X),nl,
result(X, Y),nl,
write("this is the result"),nl,
write(Y).
Output: -
Q 2. Try to trace through the search process for given questions.
friend(jin, james).
friend(jin,john).
likes(john, jin).
likes(james, john).
happy(X):-friend(X,Y),likes(Y,X).
Question :
1. Who is happy?
2. Who is unhappy ? reason.
3. Modify the above code to make john happy
Program 2: -
friend(jin, james).
friend(jin,john).
likes(john, jin).
likes(james, john).
happy(X):-friend(X,Y),likes(Y,X).
Output:-
2/7
Q 3.Try to trace through the search process for given questions.
likes(john,jane).
likes(jane,john).
likes(jack,jane).
Program 3:-
likes(john,jane).
likes(jane,john).
likes(jack,jane).
Output: -
3/7
Q 4. For given English statements write a prolog program.
- Facts & Rules
(1) jane is a female.
(2) john is a male.
(3) john is parent of mary.
(4) john is parent of bob.
(5) jane is parent of Mary.
(6) jane is parent of bob.
(7)bob is parent of ann.
(8)bob is parent of lisa.
(9) mary is a female.
(10) bob is a male.
- Goals.
(1) Who are the sibling.
(2) Who is mother of mary
Program 4: -
parent(john, mary).
parent(john, bob).
parent(jane, mary).
parent(jane, bob).
parent(bob, ann).
parent(bob, lisa).
male(john).
male(bob).
female(jane).
female(mary).
Output: -
4/7
Q 5.Write Prolog Program for Fibonacci Series.
Program 5: -
% Base cases
fib(0, 0).
fib(1, 1).
% Recursive rule
fib(N, Result) :-
N > 1,
N1 is N - 1,
N2 is N - 2,
fib(N1, F1),
fib(N2, F2),
Result is F1 + F2.
Output: -
5/7
Q 6. Write Prolog code for following FACTS.
Program 6: -
Output:
6/7
7/7